PHP Simple HTML DOM Parser

This is a neat and nice PHP library to use when crawling or analyzing HTML content based on a URL or a string.

  • A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way!
  • Require PHP 5+.
  • Supports invalid HTML.
  • Find tags on an HTML page with selectors just like jQuery.
  • Extract contents from HTML in a single line.

<?php
require_once('sites/default/thirdparty/simplehtmldom/simple_html_dom.php');

....

// Create DOM from URL or file
$html = file_get_html('<a href="http://www.google.com/'">http://www.google.com/'</a>);

// Find all images
foreach(
$html->find('img') as $element)
       echo
$element->src . '<br>';

// Find all links
foreach(
$html->find('a') as $element)
       echo
$element->href . '<br>';

...

// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');

//Set the class to 'bar' on the second div (0, 1, 2...)
$html->find('div', 1)->class = 'bar';

//Change the inner HTML text to 'foo' in the first div with id 'hello'
$html->find('div[id=hello]', 0)->innertext = 'foo';
?>
Tool type: