How to Search an RSS Feed for Any Keywords Using PHP 


               


How to Search an RSS Feed for any Keywords Using PHP

PHP







In this article, we show how to search an RSS feed for any keywords using PHP.

So RSS feeds are a way that a website has of sharing information that can be used by the rest of the internet to see what new articles they have written.

RSS stands for Really Simple Syndication.

If you look up the definition for syndication, it means the supply or broadcast content.

When a website creates an RSS feed, it puts the contents of the new articles that it publishes on the RSS feed, such as the article title, the link to the article, the publication date, and so on.

Users who are subscribed to the RSS feed then get fed this new updated content. So a user doesn't have to visit the site to see if there any new articles. Instead, through subscription to the RSS feed, a user can automatically know, through the RSS feed, when there are new articles.

Besides subscribiing to an RSS feed, another way to do it is to create a PHP program that reads an RSS feed.

This is what we'll do in this article. However, instead of just reading the RSS feed, we'll search it for a keyword. For example, say you're interested in news in Israel, you can look for articles on 'Israel.' Say if you love driverless cars and reading new updates on that, you can search 'driverless' and see which articles have new stories pertaining to that.

So searching a RSS feed isn't hard. You simply read the RSS feed and then use an if statement to see if the searh term that the user enters is in any of the titles in the RSS feed. This is how you can search any RSS feed for any term.

PHP Code

The PHP code to search an RSS feed for any keyword is shown below.



So the $rss variable simulates a DomDocument.

The $rss variable loads the CNN tech RSS feed.

This is the RSS feed for the technology section of the CNN website.

We then create an array named $list.

This will hold all of the items in the RSS feed (an item represents an article on that website).

We also create a $found variable and initalize it to 0. Later on in the code, if the keyword we are searching for is found, we increment this variable. If it isn't found, the variable remains 0. This is so that we know whether the keyword returned any results or not. If not, we can output that no results have been found.

So next in our code, we have a loop. We loop through each item in the RSS feed for a given site through the foreach loop. This foreach loop takes the $rss variables which holds represents the entire RSS feed as a document object model.

The array $list will be an associative array, that is an array composed of name/value pairs or property/value pairs. We have 4 names or properties in this associative array: title, descript, link, and date. We assign these respectively to the title of the article, the description of the article, the hyperlin to the article, and the date the article was published.

We get the values of these tags through the getElementsByTagName() function. Inside of this function, we pass the tag that we want to retrieve.

For each item in the RSS feed, we add it to the array $list via the array_push() function. This portion of the code ends when we've gone through each item in the RSS feed.

Next, we create a variable named $numberofresults. This is the number of results we show from the RSS feed. We limit it to this number.

We create a for loop just for the purpose of us being able to loop through each element that was pushed onto the array we've created, $list.

We start at the first element (element 0) and go until we have gotten 20 results (if there is 20 results to be gotten).

After this, we echo out as a header (an h1 tag) CNN Articles. This is because the articles are from CNN.

We then create a PHP variable $title, which we set equal to $list[$i]['title']. This gets the title from the $list array.

Being that we want to check to see if the title has a certain keyword, we create an if statement. If the title contains the keyword "tesla", we then get the rest of the data and output the results. If it does not contain the keyword "tesla", then the if statement is skipped.

Back to the if statement, we then create a variable named $link, which we set equal to $list[$i]['link']. This retrieves the link from the array.

We do the same thing for $description.

For the $date variable, we get the date by using the strtotime() function to convert the string in the XML RSS feed to a time value that can be formatted in PHP.

Now we go on to outputting the values through the echo() function. We output the title, link, date, and description.

Notice if the if statement is executed and the keyword "tesla" is in the title, the $found variable increments by 1 and, thus, is no longer 0.

If "tesla" is never found in the title, the if statement is never executed and, thus, the $found variable remains 0.

And this is all that is required to search an RSS feed for any keyword you are looking for using PHP.

Running the code above gives us the output seen at the following link: RSS Feed Check for Tesla.

Of course instead of putting the word "tesla" directly in the code, we can instead create an HTML text box and extract what a user enters into the text box and place this into a variable which we then check to see is in the title. This way, a user can look up anything he wants from the CNN news feed.

Also if you want to check multiple keywords, then you would add in more strpos() statements. For example, if you want to check for "Tesla" and "Gigafactory", the if statement would look like this, if ((strpos($title2, "tesla") !== false) && (strpos($title2, "tesla") !== false)) { //code inside } So this is how you can check as many keywords as you want. If you put &&, this means both keywords must be present in title in order for it to be output.

So these are some of the variations you can do when searching RSS feeds.


Related Resources

How to Read an RSS Feed Using PHP




HTML Comment Box is loading comments...