How to filter / hide / ignore News in an RSS feed

Recently I realized that there are certain topics in my news / RSS feed that I ALWAYS scroll over. I’m just not interested in them. Like news about certain brands, manufacturer or products. I wondered, if there is no easy way to simply NOT see them in my feed. I mean I filter by only looking at keywords in the title. Really super simple.

Unfortunately feedly.com does not provide any filter functionality (which is known to me). There are some other RSS reader apps that do that but I didn’t want to change away from feedly. Also I didn’t want to pay for a service that just filters by couple of keywords.

Yet the solution was so easy: use a simple PHP Script that only

  • reads the source RSS
  • filters the according items by a predefined list of words
  • dump the remaining RSS feed back to the browser
<?php
// call: https://your.example.com/filter.php?rss_feed_url=http://the.feed/to_be_filtered
// Define the unwanted list of words
$filter = array("Foo", "Bar", "Foobar");

// Get the RSS feed URL from a GET parameter
if (isset($_GET&#91;'rss_feed_url'&#93;)) {
    $rss_feed_url = $_GET&#91;'rss_feed_url'&#93;;
} else {
    echo "RSS feed URL not provided.";
    exit();
}

// Validate the RSS feed URL (You can add more robust validation as needed)
if (!filter_var($rss_feed_url, FILTER_VALIDATE_URL)) {
    echo "Invalid RSS feed URL.";
    exit();
}

$rss = simplexml_load_file($rss_feed_url);
if ($rss) {
    // Loop through the RSS feed items
	for($i=count($rss->channel->item)-1; $i >= 0; $i--){
		$item = $rss->channel->item[$i];
        // Check if the title contains any of the blacklisted words
        foreach ($filter as $word) {
            if (stripos($item->title, $word) !== false) {
                unset($rss->channel->item[$i]); // Remove the item from the XML if an unwanted word is found
                break; // and exit the loop
            }
        }
    }
 	header('Content-Type: application/rss+xml; charset=UTF-8');
	$rss->channel->title .= " - Filtered";
    echo $rss->asXML();
} else {
    echo "Failed to load and parse the RSS feed.";
}

2 thoughts on “How to filter / hide / ignore News in an RSS feed”

Leave a Reply

Your email address will not be published. Required fields are marked *