PHPackages                             rct567/dom-query - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. rct567/dom-query

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

rct567/dom-query
================

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

v1.2(5mo ago)134261.0k↑44.3%404MITPHPPHP ^7.2.0||^8.0.0

Since Oct 6Pushed 5mo ago5 watchersCompare

[ Source](https://github.com/Rct567/DomQuery)[ Packagist](https://packagist.org/packages/rct567/dom-query)[ RSS](/packages/rct567-dom-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (37)Used By (4)

DomQuery
========

[](#domquery)

DomQuery is a PHP library that allows you to easily traverse and modify the DOM (HTML/XML). As a library it aims to provide 'jQuery like' access to the PHP DOMDocument class ().

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

[](#installation)

Install the latest version with

```
$ composer require rct567/dom-query
```

Basic Usage
-----------

[](#basic-usage)

### Read attributes and properties:

[](#read-attributes-and-properties)

```
use Rct567\DomQuery\DomQuery;

$dom = new DomQuery('Hello');

echo $dom->find('h1')->text(); // output: Hello
echo $dom->find('div')->prop('outerHTML'); // output: Hello
echo $dom->find('div')->html(); // output: Hello
echo $dom->find('div > h1')->class; // output: title
echo $dom->find('div > h1')->attr('class'); // output: title
echo $dom->find('div > h1')->prop('tagName'); // output: h1
echo $dom->find('div')->children('h1')->prop('tagName'); // output: h1
echo (string) $dom->find('div > h1'); // output: Hello
echo count($dom->find('div, h1')); // output: 2
```

### Traversing nodes (result set):

[](#traversing-nodes-result-set)

```
use Rct567\DomQuery\DomQuery;

$dom = new DomQuery('1 2 3');
$links = $dom->children('a');

foreach($links as $elm) {
    echo $elm->text(); // output 123
}

echo $links[0]->text(); // output 1
echo $links->last()->text(); // output 3
echo $links->first()->next()->text(); // output 2
echo $links->last()->prev()->text(); // output 2
echo $links->get(0)->textContent; // output 1
echo $links->get(-1)->textContent; // output 3
```

### Factory method (create instance alternative):

[](#factory-method-create-instance-alternative)

```
use Rct567\DomQuery\DomQuery;

DomQuery::create('')->attr('title') // hello
```

Jquery methods available
------------------------

[](#jquery-methods-available)

#### Traversing &gt; Tree Traversal

[](#traversing--tree-traversal)

- `.find( selector )`
- `.children( [selector] )`
- `.parent( [selector] )`
- `.closest( [selector] )`
- `.next( [selector] )`
- `.prev( [selector] )`
- `.nextAll( [selector] )`
- `.prevAll( [selector] )`
- `.nextUntil( [selector] )`
- `.prevUntil( [selector] )`
- `.siblings( [selector] )`

#### Traversing &gt; Miscellaneous Traversing

[](#traversing--miscellaneous-traversing)

- `.contents()` get children including text nodes
- `.add( selector, [context] )` new result with added elements that match selector
- `.addBack()`

#### Traversing &gt; Filtering

[](#traversing--filtering)

- `.is( selector )`
- `.filter ( selector )` reduce to those that match the selector
- `.not( selector )` remove elements from the set of matched elements
- `.has( selector )` reduce to those that have a descendant that matches the selector
- `.first( [selector] )`
- `.last( [selector] )`
- `.slice( [offset] [, length])` like [array\_slice in php](http://php.net/manual/en/function.array-slice.php), not js/jquery
- `.eq( index )`
- `.map( callable(elm,i) )`

\* **\[selector\]** can be a css selector or an instance of DomQuery|DOMNodeList|DOMNode

#### Manipulation &gt; DOM Insertion &amp; removal

[](#manipulation--dom-insertion--removal)

- `.text( [text] )`
- `.html( [html_string] )`
- `.append( [content],... )`
- `.prepend( [content],... )`
- `.after( [content],... )`
- `.before( [content],... )`
- `.appendTo( [target] )`
- `.prependTo( [target] )`
- `.replaceWith( [content] )`
- `.wrap( [content] )`
- `.wrapAll( [content] )`
- `.wrapInner( [content] )`
- `.remove( [selector] )`

\* **\[content\]** can be html or an instance of DomQuery|DOMNodeList|DOMNode

#### Attributes | Manipulation

[](#attributes--manipulation)

- `.attr( name [, val] )`
- `.prop( name [, val] )`
- `.css( name [, val] )`
- `.removeAttr( name )`
- `.addClass( name )`
- `.hasClass( name )`
- `.toggleClass ( name )`
- `.removeClass( [name] )`

\* addClass, removeClass, toggleClass and removeAttr also accepts an array or space-separated **names**

#### Miscellaneous &gt; DOM Element Methods | Traversing | Storage

[](#miscellaneous--dom-element-methods--traversing--storage)

- `.get( index )`
- `.each ( callable(elm,i) )`
- `.data ( key [, val] )`
- `.removeData ( [name] )`
- `.index ( [selector] )`
- `.toArray()`
- `.clone()`

Supported selectors
-------------------

[](#supported-selectors)

- `.class`
- `#foo`
- `parent > child`
- `foo, bar` multiple selectors
- `prev + next` elements matching "next" that are immediately preceded by a sibling "prev"
- `prev ~ siblings` elements matching "siblings" that are preceded by "prev"
- `*` all selector
- `[name="foo"]` attribute value equal foo
- `[name*="foo"]` attribute value contains foo
- `[name~="foo"]` attribute value contains word foo
- `[name^="foo"]` attribute value starts with foo
- `[name$="foo"]` attribute value ends with foo
- `[name|="foo"]` attribute value equal to foo, or starting foo followed by a hyphen (-)

### Pseudo selectors

[](#pseudo-selectors)

- `:empty`
- `:even`
- `:odd`
- `:first-child`
- `:last-child`
- `:only-child`
- `:nth-child(n)`
- `:parent` elements that have at least one child node
- `:first`
- `:last`
- `:header` selects h1, h2, h3 etc.
- `:not(foo)` elements that do not match selector foo
- `:has(foo)` elements containing at least one element that matches foo selector
- `:contains(foo)` elements that contain text foo
- `:root` element that is the root of the document

Other (non jQuery) methods
--------------------------

[](#other-non-jquery-methods)

- `findOrFail( selector )` find descendants of each element in the current set of matched elements, or throw an exception
- `loadContent(content, encoding='UTF-8')` load html/xml content
- `xpath(xpath_query)` Use xpath to find descendants of each element in the current set of matched elements
- `getOuterHtml()` get resulting html describing all the elements (same as `(string) $dom`, or `$elm->prop('outerHTML')`)

XML support
-----------

[](#xml-support)

- XML content will automatically be loaded '[as XML](http://php.net/manual/en/domdocument.loadxml.php)' if a [XML declaration](http://xmlwriter.net/xml_guide/xml_declaration.shtml) is found (property `xml_mode` will be set to true)
- This in turn will also make saving (rendering) happen '[as XML](http://php.net/manual/en/domdocument.savexml.php)'. You can set property `xml_mode` to false to prevent this.
- To prevent content with a XML declaration loading 'as XML' you can set property `xml_mode` to false and then use the `loadContent($content)` method.
- Namespaces are automatically registered (no need to do it [manually](http://php.net/manual/en/domxpath.registernamespace.php))

Escaping meta chars in selector to find elements with namespace:

```
$dom->find('namespace\\:h1')->text();
```

About
-----

[](#about)

### Requirements

[](#requirements)

- Works with PHP 7.2 or above (try v0.8 for older PHP versions)
- Requires libxml PHP extension (enabled by default)

### Inspiration/acknowledgements

[](#inspirationacknowledgements)

-
-
-
-
-

###  Health Score

61

—

FairBetter than 99% of packages

Maintenance70

Regular maintenance activity

Popularity51

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 97.5% 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

Every ~85 days

Recently: every ~202 days

Total

36

Last Release

177d ago

Major Versions

v0.9 → v1.02023-09-15

PHP version history (3 changes)v0.1.4PHP ^7.0.0

v0.9PHP ^7.2.0|^8.0.0

v1.0PHP ^7.2.0||^8.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/815a190cf9551c97e82abddf6519e238e1ab1c65bcd25379da2eb38c57dc434d?d=identicon)[Rct567](/maintainers/Rct567)

---

Top Contributors

[![Rct567](https://avatars.githubusercontent.com/u/1885003?v=4)](https://github.com/Rct567 "Rct567 (347 commits)")[![joshua-graham-adelphi](https://avatars.githubusercontent.com/u/26053710?v=4)](https://github.com/joshua-graham-adelphi "joshua-graham-adelphi (6 commits)")[![jago86](https://avatars.githubusercontent.com/u/1690276?v=4)](https://github.com/jago86 "jago86 (2 commits)")[![antalaron](https://avatars.githubusercontent.com/u/9386504?v=4)](https://github.com/antalaron "antalaron (1 commits)")

---

Tags

domdomdocumentdomqueryhtmlhtmlparserjquerylibxmlphp-libraryxpathxpath-queryxmlcsshtmldomjqueryselector

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rct567-dom-query/health.svg)

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[querypath/querypath

HTML/XML querying and processing (like jQuery)

8197.0M27](/packages/querypath-querypath)[gravitypdf/querypath

PHP library for HTML(5)/XML querying (CSS 4 or XPath) and processing (like jQuery) with PHP 7.1 to 8.5 support

281.2M1](/packages/gravitypdf-querypath)[scotteh/php-dom-wrapper

Simple DOM wrapper to select nodes using either CSS or XPath expressions and manipulate results quickly and easily.

1471.9M10](/packages/scotteh-php-dom-wrapper)[fluentdom/fluentdom

A fluent api for the php dom extension.

337306.9k17](/packages/fluentdom-fluentdom)[hexydec/htmldoc

A token based HTML document parser and minifier. Minify HTML documents including inline CSS, Javascript, and SVG's on the fly. Extract document text, attributes, and fragments. Full test suite.

2610.3k3](/packages/hexydec-htmldoc)

PHPackages © 2026

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