Conditional PHP

I have this bit of PHP code that creates a list of news stories from google. I need to not have any foxnews.com domains listed.
<?php
     $tickerUrl = "http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss";

     $xml = simplexml_load_file( $tickerUrl );


     foreach( $xml->channel->item as $aNode ) {
          $title = (string) $aNode->title;
          $link  = (string) $aNode->link;
          $ticker .= "<li><a href='$link'>". htmlspecialchars($title, ENT_QUOTES) ."</a></li>&nbsp&nbsp&nbsp&nbsp&nbsp";
     }

?>

Open in new window

LVL 16
JF0Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

te-eduCommented:
You do not have complete code to understand but as I se you must use regular expressions.

<?php
     $tickerUrl = "http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss";

     $xml = simplexml_load_file( $tickerUrl );


     foreach( $xml->channel->item as $aNode ) {
preg_match("/foxnews\.com/us",$aNode ,$ matches)
if(!$matches){
          $title = (string) $aNode->title;
          $link  = (string) $aNode->link;
          $ticker .= "<li><a href='$link'>". htmlspecialchars($title, ENT_QUOTES) ."</a></li>&nbsp&nbsp&nbsp&nbsp&nbsp";
}
     }

?>

Open in new window

HackneyCabCommented:
You can't stand Fox either, huh?

There doesn't seem to be a clean, consistent element in the feed that identifies the source, so I think you'll have to try something like this:

foreach( $xml->channel->item as $aNode ) {
          if(strpos($aNode->title, "Fox News") !== false || strpos($aNode->link, "foxnews.com") !== false) {
                    continue;  // go to the next item in the foreach loop
          }
          $title = (string) $aNode->title;
          $link  = (string) $aNode->link;
          $ticker .= "<li><a href='$link'>". htmlspecialchars($title, ENT_QUOTES) ."</a></li>&nbsp&nbsp&nbsp&nbsp&nbsp";
     }

This will also mean that you miss out on stories from other agencies that have "Fox News" in the story title, but I think that's a small price to pay for banishing the Network.
JF0Author Commented:
Thanks, folks. I was under the impression that you could peek at $link and if it contained 'foxnews.com' it could be ignored from $ticker?

Both of your solutions seem to go about this in a way I did not expect.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Ray PaseurCommented:
Give us a little more to go on, please.  When I visit this link, the string "fox" is nowhere to be found.
http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss
JF0Author Commented:
Ray, that is a live RSS feed. There may not be any foxnews.com stories at the moment but there very well could be throughout the day.

My intention was to look at $link and if it contained 'foxnews.com' then omit it from being appended to $ticker
Ray PaseurCommented:
I think that is a viable solution.  You can use this after line 9 of your original post.

if (strpos($link, 'foxnews.com')) continue;

That will evaluate TRUE if the string is present and you can skip adding that one to the output

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Just curious - have you ever found a direct link to Fox News in the RSS feed?  It looks to me like all of the links are to Google.

On this iteration, I found "Fox News" in the title (but not in the URL of the link).  So after line 8 of the original post, you might want this:
if (strpos($title, 'Fox News')) continue;
JF0Author Commented:
While all of the links are google.com they also contain the real url and redirect to them. I am testing this now for several domains and it seems to be working.

I've decided to not scrub against the title because I would not want to miss a "Fox News Out of Business" article from another source. =]

***
This is a client request. Any opinions expressed are not necessarily my own.
***
hpiersonCommented:
this omits LINKS to foxnews

<?php
     $tickerUrl = "http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss";

     $xml = simplexml_load_file( $tickerUrl );


     foreach( $xml->channel->item as $aNode ) {
          $title = (string) $aNode->title;
          $link  = (string) $aNode->link;
          if (!strpos($link,"foxnews"))
          {$ticker .= "<li><a href='$link'>". htmlspecialchars($title, ENT_QUOTES) ."</a></li>&nbsp&nbsp&nbsp&nbsp&nbsp";}
     }
     
     echo "<ul>$ticker</ul>";

?>
Ray PaseurCommented:
@hpierson: Great minds think alike.  Exactly the same test at ID:34430578, but in the negative.

if (strpos($link, 'foxnews.com')) continue;
hpiersonCommented:
@ray,

sorry I missed your post.

I saw ID:34430632 as the last in the thread and was responding to that. As the author's comment suggested he didn't have a viable solution yet, I didn't read all previous comments carefully.
JF0Author Commented:
Ray_Paseur had the most straightforward answer that only affected links, not titles. It was also very to customize for multiple banished url's.

Thanks, everyone, it is much appreciated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.