Parse HTML using PHP


Jul 31, 2014

Last updated on May 10th, 2016 at 01:39 am

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Parse HTML using PHP

HTML is merely a subset of XML, so if you are aware of how to parse a xml file using php then this script will be easy to understand.

Refer these scripts below. This will be helpful for parsing xml files using php.

Simple XML Parsing Using PHP
XML Parsing Made Easy

Here we are going to parse a Webpage having simple HTML tags like table, tr etc.,

Lets name the HTML page as Simple_Webpage.html and add the below code.

                          <td><b>Temp {F}</b></td>
                          <td><b>Current Status</b></td> 
                          <td>United States</td> 
                          <td>United Kingdom</td> 


Now we are going to parse the above HTML webpage using the below php code.

<html><body><title>Parse HTML Demo</title><?php
  // new dom object
  $dom = new DOMDocument();

  //load the html
  $html = $dom->loadHTMLFile('Simple_Webpage.html');

  //discard white space 
  $dom->preserveWhiteSpace = false; 

  //the table by its tag name
  $tables = $dom->getElementsByTagName('table'); 

  //get all rows from the table
  $rows = $tables->item(0)->getElementsByTagName('tr'); 

  // loop over the table rows
  foreach ($rows as $row) 
   // get each column by tag name
      $cols = $row->getElementsByTagName('td'); 
   // echo the values  
      echo $cols->item(0)->nodeValue.'<br />'; 
      echo $cols->item(1)->nodeValue.'<br />'; 
      echo $cols->item(2)->nodeValue.'<hr>';;



VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
One thought on “Parse HTML using PHP”

Leave a Reply

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

seven + 20 =