0

我想使用 simplepie 为我的 rss 新闻做一个过滤器。所以我有这个代码:

$feed = new SimplePie();
$feed->set_feed_url(http://rss);
$feed->init();

$feed->set_cache_duration (3600);
$feed->set_timeout(30);
$feed->handle_content_type();

$countItem = 0;
foreach ($feed->get_items() as $item){
$checktitle = $item->get_permalink();
//Regex keyword filter
$pattern = '/keyword1|keyword2/';
//If the there is a keyword match, store in $matches array
if (preg_match($pattern, $checktitle)) {
    $news[$countItem]= $item;
    $countItem++;
}
}

我希望 $news 包含两个关键字,而不仅仅是一个或另一个。

4

1 回答 1

0

解决了这是解决方案:

$feed = new SimplePie();
$feed->set_feed_url(http://website.name/rss);
$feed->init();
$feed->set_cache_duration (3600);
$feed->set_timeout(30);
$feed->handle_content_type();
$countItem = 0;
foreach ($feed->get_items() as $item){
$checktitle = $item->get_permalink();
//Regex keyword filter
$pattern_one = '/keyword1/';
$pattern_two = '/keyword2/';
//If the there is a keyword match, store in $matches array
if (preg_match($pattern_one, $checktitle) && preg_match($pattern_two, $checktitle)) {
            $news[$countItem]= $item;
            $countItem++;
}
}
于 2012-10-24T13:51:21.190 回答