PHPackages                             alanvdb/html-dom - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. alanvdb/html-dom

ActiveLibrary

alanvdb/html-dom
================

HTML DOM manipulation library

v0.1(1y ago)07MITPHP

Since Aug 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Alanvdb/html-dom)[ Packagist](https://packagist.org/packages/alanvdb/html-dom)[ RSS](/packages/alanvdb-html-dom/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

HTML DOM
========

[](#html-dom)

A PHP library for manipulating HTML DOM structures.

Overview
--------

[](#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:

1. **HtmlDomFactory**: Creates and configures DOMDocument and DOMXPath instances from provided HTML strings.
2. **HtmlDomParser**: Provides methods for querying and manipulating an HTML DOM using XPath.

Installation
------------

[](#installation)

To install the library, use Composer:

```
composer require alanvdb/html-dom
```

Usage
-----

[](#usage)

### Creating and Using the DOMDocument

[](#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

[](#example)

```
use AlanVdb\Html\Factory\HtmlDomFactory;

$factory = new HtmlDomFactory();
$domDocument = $factory->createHtmlDomDocument('Hello World');
```

### Querying the DOM with XPath

[](#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

[](#example-1)

```
use AlanVdb\Html\Factory\HtmlDomFactory;
use AlanVdb\Html\HtmlDomParser;

$factory = new HtmlDomFactory();
$parser = $factory->createHtmlDomParserFromHtml('Hello World');

$element = $parser->getFirstElementWithId('test');
echo $element->textContent; // Outputs: Hello World
```

### Using the HtmlDomFactory

[](#using-the-htmldomfactory)

The `HtmlDomFactory` simplifies the creation of `DOMDocument`, `DOMXPath`, and `HtmlDomParser` instances.

#### Example

[](#example-2)

```
use AlanVdb\Html\Factory\HtmlDomFactory;

$factory = new HtmlDomFactory();
$parser = $factory->createHtmlDomParserFromHtml('Hello World');

$element = $parser->getFirstElementWithId('test');
echo $element->textContent; // Outputs: Hello World
```

API Documentation
-----------------

[](#api-documentation)

### HtmlDomFactory

[](#htmldomfactory)

#### Methods

[](#methods)

- **`createHtmlDomDocument(string $html): DOMDocument`**: Creates and returns a `DOMDocument` from the provided HTML string.
- **`createXPath(DOMDocument $document, bool $registerNodeNS = true): DOMXPath`**: Creates and returns a `DOMXPath` instance from the provided `DOMDocument`.
- **`createXPathFromHtml(string $html, bool $registerNodeNS = true): DOMXPath`**: Creates and returns a `DOMXPath` instance from the provided HTML string.
- **`createHtmlDomParserFromHtml(string $html): HtmlDomParser`**: Creates and returns a `HtmlDomParser` instance from the provided HTML string.

### HtmlDomParser

[](#htmldomparser)

#### Methods

[](#methods-1)

- **`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
-------

[](#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
-------

[](#license)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

630d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/79d5351bc1e25eb96224d5dd1bb67f5a60578d968784acf5f2c1edb897247b38?d=identicon)[alanvdb](/maintainers/alanvdb)

---

Top Contributors

[![Alanvdb](https://avatars.githubusercontent.com/u/35227520?v=4)](https://github.com/Alanvdb "Alanvdb (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alanvdb-html-dom/health.svg)

```
[![Health](https://phpackages.com/badges/alanvdb-html-dom/health.svg)](https://phpackages.com/packages/alanvdb-html-dom)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
