3

Working with Wordpress on this. I'm using Magpie to grab an RSS feed, but I only want it to display items that include a specific image. Here is the particular tag that I want to search for:

<media:thumbnail url="http://media.publicbroadcasting.net/wfpl/events/images/ourPick5.gif"/>

And here's the specific code I'm using in this application. It's basic, and I'm not incredibly adept at Magpie or PHP (I'm assuming there are more elegant ways of doing this for someone with a more robust knowledge of PHP):

<?php include_once(ABSPATH.WPINC.'/rss.php'); $feed = fetch_rss('http://www.publicbroadcasting.net/wfpl/.eventsfeed'); $items = array_slice($feed->items, 0, 4);?>

<?php if (!empty($items)) : ?>
<?php foreach ($items as $item) : ?>

<?php if (!preg_match('/<media:thumbnail\surl\="http:\/\/media.publicbroadcasting.net\/wfpl\/events\/images\/ourPick5.gif".+?>/i', $item['description'])) 
continue; ?>

    <li><a href="<?php echo $item['link']; ?>"><div class="item-headline"><?php echo $item['title']; ?></div>
    <div class="item-info"><?php echo $item['description']; ?>
    </div></a></li></ul>

<?php endforeach; ?>
<?php endif; ?>

Again, I only want to display the items with that specific image url in the "media:thumbnail" tag and discard all other items.

  • FYI, you should be using `fetch_feed()` rather than `fetch_rss()`, which uses the SimplePie API instead of Magpie. Magpie is no longer maintained. – Ryan McCue Aug 31 '12 at 01:36
  • I know, but there were some things with the RSS feed I was fetching and namespace issues that necessitated not using SimplePie. I've sinced worked around those (didn't fix them, though) and am using SimplePie. – user1631884 Sep 18 '12 at 04:45

1 Answers1

2

Add this part,

<?php if (!preg_match('/<media:thumbnail\surl\="http:\/\/media.publicbroadcasting.net\/wfpl\/events\/images\/ourPick5.gif".+?>/i', $item['description'])) 
    continue; ?>

before

<li><a href="<?php echo $item['link']; ?>"><div class="item-headline"><?php echo $item['title']; ?></div>
    <div class="item-info"><?php echo $item['description']; ?>
    </div></a></li>
Teno
  • 2,582
  • 4
  • 35
  • 57
  • The primary issue is that I need to detect whether the tag contains the specific url attribute listed above, not just whether or not it contains the tag. – user1631884 Aug 30 '12 at 09:17
  • just change the pattern for preg_match() then. For example, `(preg_match('//i', $item['description']))` – Teno Aug 30 '12 at 09:21
  • I may be missing something, but I've inserted the code and it hasn't filtered out any RSS items. They're all being displayed. Even the items that don't contain the tag with the url in question. – user1631884 Aug 30 '12 at 09:47
  • just updated the code in the post. realized i left out the "!" before "preg_match"...but now i have the opposite problem. no items are being displayed, even the items that contain the url... – user1631884 Aug 30 '12 at 10:01
  • You can try stripos() instead of preg_match() to find the url, something like `if (stripos($item['description'], 'http://media.publicbroadcasting.net/wfpl/events/images/ourPick5.gif'))` – Teno Aug 30 '12 at 10:05
  • You have to have the `!` operator by the way. Also `continue;` means to skip the iteration. That means, 'if the image is not found, skip it.' – Teno Aug 30 '12 at 10:29
  • sorry, meant to say that it's filtering everything out. – user1631884 Aug 30 '12 at 10:32
  • I would double-check the url of the image. Also you can try `htmlspecialchars($item['description']);` to see if it contains the image. – Teno Aug 30 '12 at 11:08
  • am i wrong, or is the stripos() you have here searching for the image url in the text that appears inside the tags instead of in the url attribute field of the tag? sorry in advance if that question is asinine...i am a noob after all... – user1631884 Aug 30 '12 at 11:26
  • You are right. I assumed that the tag is inside the tags. But as I view the source of the page you provided, actually it's in the node. Then however, Magpie does not retrieve tags without nodeValues. You can see it by doing `$url = 'http://www.publicbroadcasting.net/wfpl/.eventsfeed'; $rss = fetch_rss($url); print_r($rss->channel); print_r($rss->items);` So Magpie is not the tool for your purpose. A similar question is asked here. http://stackoverflow.com/questions/10306436/using-php-to-display-rss-feed-attributes – Teno Aug 30 '12 at 15:01