CosminStan Posted October 6, 2012 Share Posted October 6, 2012 Hi, I'm rather new to php so...I use simplepie to parse rss feed but i want to filter them by keywords i made this code : $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 = '/keyword1|keyword2/'; //If the there is a keyword match, store in $matches array if (preg_match($pattern, $checktitle)) { $news[$countItem]= $item; $countItem++; } } This is the filtering part it does it work but, i want all the items that contains both keywords. I'm not sure that preg_match is the right way to do this but it;s the only thing i got, so please! Quote Link to comment https://forums.phpfreaks.com/topic/269174-rss-flux-filter-simplepie/ Share on other sites More sharing options...
QuickOldCar Posted October 7, 2012 Share Posted October 7, 2012 can use a double preg_match with an AND in your IF statement $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++; } } Quote Link to comment https://forums.phpfreaks.com/topic/269174-rss-flux-filter-simplepie/#findComment-1383394 Share on other sites More sharing options...
Christian F. Posted October 7, 2012 Share Posted October 7, 2012 No need to use Regular Expressions for this, stripos () is more than adequate. By using the following example you can make it match any number of keywords, without having to change anything but the list (array) of keywords. // Build the list of keywords, and mark the string as a match by default. $keywords = array ('keyword1', 'keyword2'); $match = true; // Loop through every single keyword. foreach ($keywords as $word) { // If keyword is found in the string, continue with the next iteration. if (stripos ($string, $word) !== false) { continue; } // Keyword wasn't found, so mark the match as false and stop the loop. $match = false; break; } Quote Link to comment https://forums.phpfreaks.com/topic/269174-rss-flux-filter-simplepie/#findComment-1383432 Share on other sites More sharing options...
CosminStan Posted October 7, 2012 Author Share Posted October 7, 2012 @Christian F. so in my case i have to do 2 foreach, right ? one for the feed and the other for the keyword Quote Link to comment https://forums.phpfreaks.com/topic/269174-rss-flux-filter-simplepie/#findComment-1383441 Share on other sites More sharing options...
Christian F. Posted October 7, 2012 Share Posted October 7, 2012 Yes, if you have multiple strings you want to match against the keywords, you'll need to wrap my code inside a loop that runs through the strings. Quote Link to comment https://forums.phpfreaks.com/topic/269174-rss-flux-filter-simplepie/#findComment-1383469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.