I have a problem with customizing an rss feed by taking advantage of the php preg_match function to extract some particular content out of the description for the feed item.
Since I am not a programmer, but try to make it work anyway, I would appreciate if somebody with php knowledge could explain where I am going wrong.
Some comments before I post the code:
- echo $item->get_title(); works as expected
- echo $item->get_description(); also works correctly and shows me the entire description block for the feed item. (I don’t echo the description in the example below, I just tested to make sure the description is available for my string variable.
I need to drill down to the element in the description that shows me the Priority level of the item. This info sits in the fourth span element of the feed item. I want to use the function getTextBetweenTags() to extract the text within the fourth span item. But when I try to echo the text, it doesn’t work.
So my question is how to correctly use the function getTextBetweenTags so that I can actually display the extracted text.
Here’s what I have:
<?php
include_once(ABSPATH . WPINC . '/feed.php'); //path to SimplePie
$rss = fetch_feed('http://www.rememberthemilk.com/atom/vrod_2000/14260889/?tok=eJwNxlEKQkEIBdAVPXCu46irCR0dCIKgovXX*TqlPjPViI4liROXHzCtFkGXjmmx0cLHG7HIUb5zjYqED5zr*3rWDUR0Pe7vzzUmFpn5RQmNNCw9JhiqyosPWe0UmqzRu8Wx*l-I2EmdGqpFHMETP3O9KWA'); // specify feed url
if(!empty($rss)):
$maxitems = $rss->get_item_quantity(50);
$rss_items = $rss->get_items(0, $maxitems);
//set up function to find the fourth span tag in the description of the feed item
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[3];
}
endif;
?>
<!--start output of feed items on page-->
<ul>
<?php if ($maxitems == 0) echo '<li>There are no tasks pending at this time.</li>';
else
foreach ( $rss_items as $item ) :?>
<li class="tasklist"><strong><?php echo $item->get_title(); //show feed item title
$string = $item->get_description(); //variable containing all the span tags I am looking for in the item description
echo getTextBetweenTags($string, 'span'); //show feed item priority level (located in 4th span tag)
?>
</strong> </li>
<?php endforeach; ?>
</ul>