A small RSS filter written in PHP

Nearly all news sources has some posts I’m not interested in. I can’t believe no RSS readers has implemented good enough filters yet. But I anyway I wrote my own. :) I don’t think my server can handle too much usage, so I’m just giving you the code as is.

<?php

$xml =  simplexml_load_file($_GET['input']);

$ix = 0;
while ($ix < count($xml->channel->item) ) {
  if (!array_in_array($xml->channel->item[$ix]->category,$_GET['categories'])) {
    unset($xml->channel->item[$ix]);
  } else {
    $ix++;
  }
} 

echo $xml->asXML();

// in_array that accepts two arrays (and returns true if it finds any common value)
function array_in_array($a, $b) {
  if (!is_array($a))
    return in_array($a, $b);

  foreach ($a as $elem) {
    if (in_array($elem, $b))
      return true;
  }
  return false;
}

?>

Install to a PHP5 server and use like this (fictional example):

http://yourserver.com/rssfilter/index.php?input=http%3A%2F%2Fvg.no%2Ffeed&categories%5B%5D=Sport&categories%5B%5D=Rampelys

The script returns a RSS feed with only entries in one of the chosen categories. It does no caching or header support, so it uses much more resources than necessary. That’s why I don’t tell where my personal installation is. :)

Leave a comment

3 Comments

  1. slee

     /  28. March, 2011

    hello, i found your page by googling ‘rss filter’…this is similar to what i’m trying to accomplish.

    i’m trying to remove certain items from the rss(xml) by keyword so i can use:

    http://myip.com/rssfilter?http://somefeed.com

    here is my latest attempt on it:
    <?php
    $lnk="http://someatomfeed/atom.xml&quot;;
    $pg=file_get_contents($lnk);
    $pg = preg_replace('#.*?Great.*?#s’, ”, $pg);
    echo $pg;
    ?>

    where it removes an ‘*great* from my $pg xml content
    any help would be greatly appreciated

    thanks

  2. slee

     /  28. March, 2011

    ugh, posting the php code in comments stripeed out the tags, let me try and re explain..i’m downloading an rss feed, it has multiple ‘entry’ tags seperating each item of news, i’m wanting to remove each ‘entry’ if it contains a keyword anywhere in the $pg

    thanks again

  3. Using preg_replace on an XML document is begging for trouble. I think you need to use simplexml_load_file() like I do and parse the XML properly.

Leave a Reply