Category Archives: parsing

Google Hot/Top Trends By Countries : PHP implementation

Hey there,

Google Trends is a public web facility of Google Inc., based on Google Search, that shows how often a particular search term is entered relative to the total search volume across various regions of the world, and in various languages. Google Hot/Top Trends displays the top 20 hot, i.e., fastest rising, searches (search-terms) of the past hour from many countries around the Globe. This is for searches that have recently experienced a sudden surge in popularity. I was in search for finding top keywords for one of my online service Social Media Search. Somehow I managed to get Google trends atom feed URL from my Stackoverflow. The atom feed URL which I got from Stackoverflow was for Google Global Hot Trends (United States) & I had collected other Google top level country domains to integrate with the PHP version of  Google hot trends by Countries.

I’m giving the details below:

Google Trends Class (googleTrends.php), Which is really diminutive.

Usage :

Click here to download complete source code from GitHub

Share it..Cheers..:)

Simple HTML parsing using PHP DomDocument With Example

Hey there,

Here I’m just giving an example about how to parse a HTML document with the help of PHP’s DomDocument class. This example is intended for a beginner, with little or limited knowledge about parsing HTML using PHP. In this example I’m using this web page (Included in the Source Code Zip) as a HTML document. This web page is a list of all top country level domains owned by Google & related details (which include Extension, Country Name, Language & ISO etc.). We’ll look into how to fetch all those data using DomDocument class. If you refer to the HTML source, you could see all those data like Top level domain, Extension contained with in <td> elements within different <tr> elements . So our main task is to fetch all <td> values. I’m giving the PHP code used to fetch those data.

I’ll explain the source code below :

Initially I’ve created an object variable ($dom) for DOMDocument class

Here used PHP’s file_get_contents function to fetch the HTML content.

Loading HTML source using DomDocument loadHTML function.

Get all <tr> elements

Loop through all <td> elements & respective elements to fetch all required data.

You could see I’ve used getElementsByTagName again to fetch all <td> elements as well. If you execute this PHP code, you can print all values contained within all <td> elements.

That’s it. You can Download the source Here.  Share it..Cheers..:)

Google Image Search API With PHP

Hey there,

For One of my Online Service, I wanted to provide some image results apart from my real output. So I had to search for any of the Image search API with PHP. But I didn’t get any proper solution for my requirement. Finally I’ve modified one of the Image API URL (Eg. https://www.google.com/uds/GimageSearch?rsz=8&q=sachin+tendulkar&v=1.0&start=0&imgsz=small) which I got after in depth searching. The PHP Source is really small & simple. In PHP code, I’ve used

function to read JSON response returned from the Image search API &

function (with second parameter set to true) converts the JSON string into array. You can process this final array according to your requirement.

Click Here for Live Demo & Source Code

GitHub Repository

Hope it’ll be useful for you. Share it. Cheers..:)

PHP How to remove numbers from string?

Hey there,

While parsing any document (HTML, XML or aything!), You might have involved in a stage where you would need to remove numeric values (numbers) from the string. PHP has some useful function which will make this easy as butter..:)

The above PHP code makes use of the functions range(), str_replace(), trim().

range() : Create an array containing a range of elements

str_replace() : Replace all occurrences of the search string with the replacement string

trim() : Strip whitespace (or other characters) from the beginning and end of a string

That’s all. Share it..Enjoy Coding..Cheers..:)

 

XML Parsing with PHP DomDocument : Examples

Sample XML file for testing



Getting all Car names from all persons:

Getting all Car colors from all persons:

Joining Car names & Colors together for all persons: