How to correctly call that function?

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>


Changing to $matches[1] actually now gets me some output, just still not what I want. Here’s the string that I am working with (where content in the entry element is the feed description). $matches[1] returns the text within the first span, which is “Due:”. $matches[2] returns nothing, I thought it would get me “Thu 29 Jul 10”. I want to get to the “1” in <span class=“rtm_priority_value”>1</span>.


<entry>
...
<div class="rtm_due">
<span class="rtm_due_title">Due: </span>
<span class="rtm_due_value">Thu 29 Jul 10</span>
</div>
<div class="rtm_priority">
<span class="rtm_priority_title">Priority: </span>
<span class="rtm_priority_value">1</span>
</div>
...
</entry>


function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[3];
}

Looks to like you want the first backreference, so it should be return $matches[1]; instead of return $matches[3];

:slight_smile:

I was able to figure this out (finding the preg_match_all function in the php manual)


 preg_match_all($pattern, $string, $matches);
 return $matches[1][3];

is working. Yeah!