Skip to content

simple-html-domvshtml5-php

MIT - - -
2.0 thousand (month) Nov 09 2019 2.0-RC2(2019-11-09 15:42:50 ago)
1,790 6 32 MIT
Jun 01 2013 559.1 thousand (month) 2.12.0(2026-10-25 08:57:58 ago)

Simple HTML DOM Parser is a lightweight PHP library for parsing and manipulating HTML documents using a jQuery-like syntax. It is one of the most widely used HTML parsers in the PHP ecosystem, known for its simplicity and low learning curve.

Key features include:

  • jQuery-like selectors Find elements using CSS selectors similar to jQuery: find('#id'), find('.class'), find('div > p'), etc.
  • Easy element access Access element attributes, inner text, and inner HTML with simple property access: $element->plaintext, $element->href, $element->innertext.
  • DOM manipulation Modify element attributes, text content, and HTML. Add or remove elements from the document tree.
  • File and string parsing Parse HTML from strings, local files, or URLs directly.
  • Nested element traversal Navigate parent, child, and sibling elements with built-in traversal methods.
  • Memory-friendly Designed to handle large HTML documents efficiently with explicit memory cleanup.

Simple HTML DOM is best suited for quick scraping tasks and small projects where simplicity is more important than performance. For large-scale or performance-critical applications, consider using PHP's built-in DOMDocument or Symfony's DomCrawler which offer better performance and standards compliance.

HTML5 is a standards-compliant HTML5 parser and writer written entirely in PHP. It is stable and used in many production websites, and has well over five million downloads.

HTML5 provides the following features:

  • An HTML5 serializer
  • Support for PHP namespaces
  • Composer support
  • Event-based (SAX-like) parser
  • A DOM tree builder
  • Interoperability with QueryPath
  • Runs on PHP 5.3.0 or newer

Note that html5-php is a low-level HTML parser and does not feature any query features like CSS selectors.

Highlights


css-selectorsdsl-selectors

Example Use


<?php
require 'vendor/autoload.php';

use simplehtmldom\HtmlWeb;
use simplehtmldom\HtmlDocument;

// Parse HTML from a URL
$web = new HtmlWeb();
$html = $web->load('https://example.com');

// Or parse from a string
$html = new HtmlDocument('<div><p class="intro">Hello World</p></div>');

// Find elements using CSS-like selectors
$title = $html->find('title', 0)->plaintext;
echo "Title: $title\n";

// Find all products
foreach ($html->find('.product') as $product) {
    $name = $product->find('.name', 0)->plaintext;
    $price = $product->find('.price', 0)->plaintext;
    $link = $product->find('a', 0)->href;
    echo "$name: $price ($link)\n";
}

// Modify elements
$html->find('h1', 0)->innertext = 'Modified Title';

// Clean up memory when done
$html->clear();
<?php
// Assuming you installed from Composer:
require "vendor/autoload.php";

use Masterminds\HTML5;

// An example HTML document:
$html = <<< 'HERE'
  <html>
  <head>
    <title>TEST</title>
  </head>
  <body id='foo'>
    <h1>Hello World</h1>
    <p>This is a test of the HTML5 parser.</p>
  </body>
  </html>
HERE;

// Parse the document. $dom is a DOMDocument.
$html5 = new HTML5();
$dom = $html5->loadHTML($html);

// Render it as HTML5:
print $html5->saveHTML($dom);

// Or save it to a file:
$html5->save($dom, 'out.html');

Alternatives / Similar


Was this page helpful?