从RSS提要中提取特定条目[PHP]
因此,我有一个RSS提要,每个项目都有不同的内容。我想做的只是获取包含特定文本部分的条目 例如:从RSS提要中提取特定条目[PHP],php,xml,regex,rss,preg-match,Php,Xml,Regex,Rss,Preg Match,因此,我有一个RSS提要,每个项目都有不同的内容。我想做的只是获取包含特定文本部分的条目 例如: <item> <title>RADIO SHOW - CF64K - 05-20-10 + WRAPUP </title> <link>http://linktoradioshow.com</link> <comments>Radio show from 05-20-10</comments>
<item>
<title>RADIO SHOW - CF64K - 05-20-10 + WRAPUP </title>
<link>http://linktoradioshow.com</link>
<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">Audio / Other</category>
<dc:creator>n0s</dc:creator>
<guid>http://otherlinktoradioshow.com/</guid>
<enclosure url="http://linktoradioshow.com/" length="13005" />
</item>
<item>
<title>RADIO SHOW - CF128K - 05-20-10 + WRAPUP </title>
<link>http://linktoradioshow.com</link>
<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">Audio / Other</category>
<dc:creator>n0s</dc:creator>
<guid>http://otherlinktoradioshow.com/</guid>
<enclosure url="http://linktoradioshow.com/" length="13005" />
</item>
广播节目-CF64K-05-20-10+WRAPUP
http://linktoradioshow.com
2010年5月20日的广播节目
2010年5月20日星期四19:12:12+0200
音频/其他
n0s
http://otherlinktoradioshow.com/
广播节目-CF128K-05-20-10+WRAPUP
http://linktoradioshow.com
2010年5月20日的广播节目
2010年5月20日星期四19:12:12+0200
音频/其他
n0s
http://otherlinktoradioshow.com/
我只想显示包含字符串CF64K
的结果。虽然它可能是非常简单的正则表达式,但我似乎无法集中精力来正确使用它。我总是觉得似乎只能显示字符串“CF64K”,而不能显示它周围的内容
提前谢谢 我猜想(因为您向我们展示了您试图解析的数据,而不是您试图解析它的代码),问题在于您试图用正则表达式解析XML。不要,这不适合它
使用RSS解析器。使用它提供的API对条目进行循环。检查它们是否符合您的要求(使用简单的字符串匹配,而不是正则表达式)。处理完成的子字符串,对于不完成的子字符串,跳回到循环的顶部。如果需要一个简单的子字符串匹配,那么可以使用XPath:
$rss = simplexml_load_file($url);
foreach ($rss->xpath('//item[contains(title, "CF64K")]') as $item)
{
print_r($item);
}
否则,您只需在项目上循环并手动筛选它们
$rss = simplexml_load_file($url);
foreach ($rss->xpath('//item') as $item)
{
if (!preg_match('#CF64K#i', $item->title))
{
continue;
}
print_r($item);
}