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..:)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">