Link to home
Create AccountLog in
Avatar of JF0
JF0Flag for United States of America

asked on

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

SOLUTION
Avatar of te-edu
te-edu
Flag of Serbia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of JF0

ASKER

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.
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
Avatar of JF0

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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;
Avatar of JF0

ASKER

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.
***
Avatar of hpierson
hpierson

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>";

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

if (strpos($link, 'foxnews.com')) continue;
@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.
Avatar of JF0

ASKER

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.