Skip to content

simple-html-domvsxpath

MIT - - -
2.0 thousand (month) Nov 09 2019 2.0-RC2(2019-11-09 15:42:50 ago)
742 3 27 MIT
Jun 08 2019 58.1 thousand (month) v1.3.9(2026-10-05 05:37:06 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.

xpath is a library for Go that allows you to use XPath expressions to select elements from an HTML document. It is built on top of the html package in the Go standard library, and provides a way to select elements from an HTML document using XPath expressions, which are more powerful and expressive than 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();
package main

import (
  "fmt"
  "github.com/antchfx/xpath"
  "golang.org/x/net/html"
  "strings"
)

func main() {
  // Create an HTML string
  html := `<html>
        <body>
          <div id="content">
            <p>Hello, World!</p>
            <a href="http://example.com">Example</a>
          </div>
        </body>
      </html>`

  // Parse the HTML string into a node tree
  doc, err := html.Parse(strings.NewReader(html))
  if err != nil {
    fmt.Println("Error:", err)
    return
  }

  // Compile the XPath expression
  expr, err := xpath.Compile("//p")
  if err != nil {
    fmt.Println("Error:", err)
    return
  }

  // Use the Evaluate method to select elements from the document
  nodes, err := expr.Evaluate(xpath.NodeNavigator(doc))
  if err != nil {
    fmt.Println("Error:", err)
    return
  }
  if nodes.MoveNext() {
    fmt.Println(nodes.Current().Value())
    // > Hello, World!
  }
}

Alternatives / Similar


Was this page helpful?