alanvdb / html-dom
HTML DOM manipulation library
Installs: 7
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/alanvdb/html-dom
Requires (Dev)
- phpunit/phpunit: ^11.3
This package is auto-updated.
Last update: 2025-09-25 13:09:45 UTC
README
A PHP library for manipulating HTML DOM structures.
Overview
The alanvdb/html-dom library provides a set of tools for working with HTML DOM in PHP. This library allows you to load HTML, traverse and manipulate DOM elements, and query elements using XPath. It is designed to simplify DOM manipulation tasks in PHP applications.
The main components of this library include:
- HtmlDomFactory: Creates and configures DOMDocument and DOMXPath instances from provided HTML strings.
- HtmlDomParser: Provides methods for querying and manipulating an HTML DOM using XPath.
Installation
To install the library, use Composer:
composer require alanvdb/html-dom
Usage
Creating and Using the DOMDocument
The HtmlDomFactory class allows you to create a DOMDocument from an HTML string. You can then use this DOMDocument to perform various operations on the DOM.
Example
use AlanVdb\Html\Factory\HtmlDomFactory; $factory = new HtmlDomFactory(); $domDocument = $factory->createHtmlDomDocument('<div id="test">Hello World</div>');
Querying the DOM with XPath
The HtmlDomParser class enables you to query the DOM using XPath expressions. You can retrieve elements by ID, class name, or tag name, and perform more complex queries.
Example
use AlanVdb\Html\Factory\HtmlDomFactory; use AlanVdb\Html\HtmlDomParser; $factory = new HtmlDomFactory(); $parser = $factory->createHtmlDomParserFromHtml('<div id="test">Hello World</div>'); $element = $parser->getFirstElementWithId('test'); echo $element->textContent; // Outputs: Hello World
Using the HtmlDomFactory
The HtmlDomFactory simplifies the creation of DOMDocument, DOMXPath, and HtmlDomParser instances.
Example
use AlanVdb\Html\Factory\HtmlDomFactory; $factory = new HtmlDomFactory(); $parser = $factory->createHtmlDomParserFromHtml('<div id="test">Hello World</div>'); $element = $parser->getFirstElementWithId('test'); echo $element->textContent; // Outputs: Hello World
API Documentation
HtmlDomFactory
Methods
createHtmlDomDocument(string $html): DOMDocument: Creates and returns aDOMDocumentfrom the provided HTML string.createXPath(DOMDocument $document, bool $registerNodeNS = true): DOMXPath: Creates and returns aDOMXPathinstance from the providedDOMDocument.createXPathFromHtml(string $html, bool $registerNodeNS = true): DOMXPath: Creates and returns aDOMXPathinstance from the provided HTML string.createHtmlDomParserFromHtml(string $html): HtmlDomParser: Creates and returns aHtmlDomParserinstance from the provided HTML string.
HtmlDomParser
Methods
getFirstElementWithId(string $id, ?DOMNode $contextNode = null, bool $registerNodeNS = true): ?DOMElement: Retrieves the first element with the specified ID.getElementsWithClass(string $className, ?DOMNode $contextNode = null, bool $registerNodeNS = true): ?DOMNodeList: Retrieves all elements with the specified class name.getFirstElementWithClass(string $className, ?DOMNode $contextNode = null, bool $registerNodeNS = true): ?DOMElement: Retrieves the first element with the specified class name.getElementsWithTag(string $tag, ?DOMNode $contextNode = null, bool $registerNodeNS = true): ?DOMNodeList: Retrieves all elements with the specified tag name.getFirstElementWithTag(string $tag, ?DOMNode $contextNode = null, bool $registerNodeNS = true): ?DOMElement: Retrieves the first element with the specified tag name.xPathQuery(string $query, ?DOMNode $contextNode = null, bool $registerNodeNS = true): ?DOMNodeList: Executes an XPath query on the DOM and returns the result.hasClass(string $class, DOMElement $element): bool: Checks if an element has a specific class.
Testing
To run the tests, use the following command:
vendor/bin/phpunit
The tests are located in the tests directory and cover the functionality of the main components such as HtmlDomFactory and HtmlDomParser.
License
This project is licensed under the MIT License. See the LICENSE file for details.