Parse HTML using PHP
Parse HTML using PHP
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.
<html> <body> <table><tr> <td><b>Country</b></td> <td><b>Temp {F}</b></td> <td><b>Current Status</b></td> </tr> <tr> <td>United States</td> <td>74</td> <td>Sunny</td> </tr> <tr> <td>United Kingdom</td> <td>65</td> <td>Sunny</td> </tr> <tr> <td>India</td> <td>94</td> <td>Sunny</td> </tr> </table> </body> </html>
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>';; } ?> </body> </html>
Great job done! Thanks for sharing .