views:

19

answers:

3

So, I have an RSS feed with variations of each item. What I want to do is just get entries that contain a specific section of text.

For example:

 <item>
    <title>RADIO SHOW - CF64K - 05-20-10 + WRAPUP </title>
    <link>http://linktoradioshow.com&lt;/link&gt;
 <comments>Radio show from 05-20-10</comments>
 <pubDate>Thu, 20 May 2010 19:12:12 +0200</pubDate>
 <category domain="http://linktoradioshow.com/browse/199"&gt;Audio / Other</category>
 <dc:creator>n0s</dc:creator>
 <guid>http://otherlinktoradioshow.com/&lt;/guid&gt;
 <enclosure url="http://linktoradioshow.com/" length="13005" />
 </item>
 <item>
 <title>RADIO SHOW - CF128K - 05-20-10 + WRAPUP </title>
 <link>http://linktoradioshow.com&lt;/link&gt;
 <comments>Radio show from 05-20-10</comments>
 <pubDate>Thu, 20 May 2010 19:12:12 +0200</pubDate>
 <category domain="http://linktoradioshow.com/browse/199"&gt;Audio / Other</category>
 <dc:creator>n0s</dc:creator>
 <guid>http://otherlinktoradioshow.com/&lt;/guid&gt;
 <enclosure url="http://linktoradioshow.com/" length="13005" />
 </item>

I only want to display the results that contain the string CF64K. While it's probably really simple regex, I can't seem to wrap my head around getting it right. I always get seem to only be able to display the string 'CF64K', and not the stuff that surrounds it.

Thanks in advance.

+1  A: 

I'm guessing (because you are showing us the data you are trying to parse but not the code you are trying to parse it with) that the problem is that you're trying to parse XML with a regular expression. Don't, it isn't suited to it.

Use an RSS parser. Loop over the entries using the API it provides. Check to see if they match your requirement (using a simple string match, not a regular expression). Process the ones that do, and skip back to the top of the loop for those that don't.

David Dorward
+1  A: 

If what you need is a simple substring match, then you can use XPath:

$rss = simplexml_load_file($url);
foreach ($rss->xpath('//item[contains(title, "CF64K")]') as $item)
{
    print_r($item);
}

Otherwise, you can just loop over the items and filter them manually

$rss = simplexml_load_file($url);
foreach ($rss->xpath('//item') as $item)
{
    if (!preg_match('#CF64K#i', $item->title))
    {
        continue;
    }
    print_r($item);
}
Josh Davis
A: 

Thanks a ton, to both of you.

Finally got it working, combining both solutions. :D

n0s