使用正则表达式过滤 RSS 链接


Filtering RSS links with regular expressions

我有点菜鸟,但一直在用本地主机上的 php 构建一个网站。 我遇到的问题是我无法弄清楚如何过滤链接中包含正则表达式的 RSS 内容。

我使用 PHP 显示 RSS 提要的代码:

<?php
 ///// RSS FEED CODE
 function getFeed1($feed_url) { 
     $content = file_get_contents($feed_url);
     $x = new SimpleXmlElement($content);
     echo "<ul>";   
         foreach($x->channel->item as $entry) {
         echo "<li><a href='$entry->link'     title='$entry->title'>" . $entry->title . "</a></li>";
         }
    echo "</ul>";
}   
getFeed1("http://www.drf.com/feeds/all-articles-of-track/SA");
?>

结果在浏览器中显示为指向页面的链接,

 Espinoza wins George Woolf Memorial Jockey Award
 Dortmund will get month to clear up foot problem
 Abrams hopes McHeat stays hot for Sensational Star
 Santa Anita attendance up, handle down
 Hot Market returns from long absence on hillside turf course
 Moon Over Paris, Divina Comedia key to pick six
 Millionaire Alert Bay looks to pad bankroll in Sensational Star
 Santa Anita to replace turf course this summer
 Free: Santa Anita horses to watch for week of Feb. 22
 Iron Rob vanned off after winning Baffle Stakes

我正在尝试弄清楚如何使用 if 语句来过滤掉以"http://www.drf.com/news/preview/"开头的链接(href)。

因此,结果将如下所示:

 Espinoza wins George Woolf Memorial Jockey Award
 Santa Anita attendance up, handle down
 Millionaire Alert Bay looks to pad bankroll in Sensational Star
 Santa Anita to replace turf course this summer
 Iron Rob vanned off after winning Baffle Stakes

在过去的两天里,我尝试了以下不同的变体:

 if (strpos($x, 'http://www.drf.com/news/preview/') !== false) 

 if (preg_match('http://www.drf.com/news/preview/', $x))        

然而,我无法正确理解语法,或者我在某处搞砸了。

发现建议使用第三方过滤器或死的雅虎管道的帖子,但我有一种感觉,我所寻求的可以通过 if 语句来完成。 我还没有找到任何可以使用正则表达式解析出 rss href 的东西。

对于了解 php 的人来说,我错过了什么? 在过去的两天里,我一直在谷歌上搜索并尝试互联网上提到的不同事情,但无济于事。 我知道追逐总是比捕获更好,但我失去了猎物的踪迹。 请帮助指出我和其他找到这篇文章的人找到踪迹。

谢谢

这是您要查找的正则表达式:

/^(http':'/'/www'.drf'.com'/news'/preview'/)/i

你也应该接受HTTPS,但要稍作修改:

/^(https?':'/'/www'.drf'.com'/news'/preview'/)/i

并且不要依赖 www 子域!

/^(https?':'/'/(www'.)?drf'.com'/news'/preview'/)/i