PHP DOM Wrapper is a simple DOM wrapper library to manipulate and traverse HTML documents. Based around jQuery's manipulation and traversal methods, largely mimicking the behaviour of it's jQuery counterparts.
- Andrew Scott (andrew@andrewscott.net.au)
- PHP 7.1 or later
- PSR-4 compatible autoloader
Install with Composer.
composer require scotteh/php-dom-wrapper This library requires an autoloader, if you aren't already using one you can include Composers autoloader.
require 'vendor/autoload.php';| Method | jQuery Equivalent (if different) |
|---|---|
| addClass() | |
| after() | |
| append() | |
| appendTo() | |
| attr() | |
| before() | |
| clone() | |
| detach() | |
| empty() | |
| hasClass() | |
| html() | |
| prepend() | |
| prependTo() | |
| remove() | |
| removeAttr() | |
| removeClass() | |
| replaceWith() | |
| text() | |
| unwrap() | |
| wrap() | |
| wrapAll() | |
| wrapInner() |
| Method | jQuery Equivalent (if different) |
|---|---|
| add() | |
| children() | |
| closest() | |
| contents() | |
| eq() | |
| filter() | |
| find() | |
| first() | |
| has() | |
| is() | |
| last() | |
| map() | |
| following() | next() |
| followingAll() | nextAll() |
| followingUntil() | nextUntil() |
| not() | |
| parent() | |
| parents() | |
| parentsUntil() | |
| preceding() | prev() |
| precedingAll() | prevAll() |
| precedingUntil() | prevUntil() |
| siblings() | |
| slice() |
| Method | jQuery Equivalent (if different) |
|---|---|
| count() | length (property) |
| each() |
Example #1:
use DOMWrap\Document; $html = '<ul><li>First</li><li>Second</li><li>Third</li></ul>'; $doc = new Document(); $doc->html($html); $nodes = $doc->find('li'); // Returns '3' var_dump($nodes->count()); // Append as a child node to each <li> $nodes->append('<b>!</b>'); // Returns: <html><body><ul><li>First<b>!</b></li><li>Second<b>!</b></li><li>Third<b>!</b></li></ul></body></html> var_dump($doc->html());self addClass(string|callable $class) $doc = (new Document())->html('<p>first paragraph</p><p>second paragraph</p>'); $doc->find('p')->addClass('text-center');Result:
<p class="text-center">first paragraph</p><p class="text-center">second paragraph</p>self after(string|NodeList|\DOMNode|callable $input) $doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>'); $doc->find('li')->after('<span> (after)</span>'); Result:
<ul><li>first<span> (after)</span></li><li>second<span> (after)</span></li></ul>self append(string|NodeList|\DOMNode|callable $input) $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>'); $doc->find('div')->append('<strong> Appended!</strong>');Result:
<div>The quick brown fox jumps over the lazy dog<strong> Appended!</strong></div>self appendTo(string|NodeList|\DOMNode $selector) $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>'); $doc->create('<strong> Appended!</strong>')->appendTo('div');Result:
<div>The quick brown fox jumps over the lazy dog<strong> Appended!</strong></div>self|string attr(string $name[, mixed $value = null]) $doc = (new Document())->html('<div class="text-center"></div>'); $doc->attr('class', 'text-left');Result:
<div class="text-left"></div>$doc = (new Document())->html('<div class="text-center"></div>'); echo $doc->attr('text-center');Result:
text-centerself before(string|NodeList|\DOMNode|callable $input) $doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>'); $doc->find('li')->after('<span>(before) </span>'); Result:
<ul><li><span>(before) </span>first</li><li><span>(before) </span>second</li></ul>NodeList|\DOMNode clone() $doc = (new Document())->html('<ul><li>Item</li></ul>'); $doc->find('div')->clone()->appendTo('ul'); Result:
<ul><li>Item</li><li>Item</li></ul>NodeList detach([string $selector = null]) $doc = (new Document())->html('<ul class="first"><li>Item</li></ul><ul class="second"></ul>'); $el = $doc->find('ul.first li')->detach(); $doc->first('ul.second').append($el); Result:
<ul class="first"></ul><ul class="second"><li>Item</li></ul>self empty() $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>'); $doc->find('div')->empty(); Result:
<div></div>bool hasClass(string $class) $doc = (new Document())->html('<div class="text-center"></div>'); echo $doc->first('div')->hasClass('text-center');Result:
truestring|self html([string|NodeList|\DOMNode|callable $input = null]) $doc = (new Document()); $doc->html('<div class="example"></div>');Result:
<div class="example"></div>$doc = (new Document())->html('<div class="example"></div>'); $doc->find('div')->append('<span>Example!</span>'); echo $doc->html();Result:
<div class="example"><span>Example!</span></div>self prepend(string|NodeList|\DOMNode|callable $input) $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>'); $doc->find('div')->prepend('<strong>Prepended! </strong>');Result:
<div><strong>Prepended! </strong>The quick brown fox jumps over the lazy dog</div>self prependTo(string|NodeList|\DOMNode $selector) $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>'); $doc->create('<strong>Prepended! </strong>')->appendTo('div');Result:
<div><strong>Prepended! </strong>The quick brown fox jumps over the lazy dog</div>self remove([string $selector = null]) $doc = (new Document())->html('<ul><li class="first"></li><li class="second"></li></ul>'); $doc->find('.first').remove();Result:
<ul><li class="second"></li></ul>self removeAttr(string $name) $doc = (new Document())->html('<div class="first second"></div>'); $doc->find('div').removeAttr('class');Result:
<div></div>self removeClass(string|callable $class) $doc = (new Document())->html('<div class="first second"></div>'); $doc->find('div').removeClass('first');Result:
<div class="second"></div>self replaceWith(string|NodeList|\DOMNode|callable $input) string|self text([string|NodeList|\DOMNode|callable $input = null]) self unwrap() self wrap(string|NodeList|\DOMNode|callable $input) self wrapAll(string|NodeList|\DOMNode|callable $input) self wrapInner(string|NodeList|\DOMNode|callable $input) NodeList add(string|NodeList|\DOMNode $input) Add additional node(s) to the existing set.
$nodes = $doc->find('a'); $nodes->add($doc->find('p'));NodeList children() Return all children of each element node in the current set.
$nodes = $doc->find('p'); $childrenOfParagraphs = $nodes->children();Element|NodeList|null closest(string|NodeList|\DOMNode|callable $input) Return the first element matching the supplied input by traversing up through the ancestors of each node in the current set.
$nodes = $doc->find('a'); $closestAncestors = $nodes->closest('p');NodeList contents() Return all children of each node in the current set.
$nodes = $doc->find('p'); $contents = $nodes->contents();\DOMNode|null eq(int $index) Return node in the current set at the specified index.
$nodes = $doc->find('a'); $nodeAtIndexOne = $nodes->eq(1);NodeList filter(string|NodeList|\DOMNode|callable $input) Return nodes in the current set that match the input.
$nodes = $doc->filter('a') $exampleATags = $nodes->filter('[href*=https://example.org/]');NodeList find(string $selector[, string $prefix = 'descendant::']) Return the decendants of the current set filtered by the selector and optional XPath axes.
$nodes = $doc->find('a');mixed first() Return the first node of the current set.
$nodes = $doc->find('a'); $firstNode = $nodes->first();NodeList has(string|NodeList|\DOMNode|callable $input) Return nodes with decendants of the current set matching the input.
$nodes = $doc->find('a'); $anchorTags = $nodes->has('span');bool is(string|NodeList|\DOMNode|callable $input) Test if nodes from the current set match the input.
$nodes = $doc->find('a'); $isAnchor = $nodes->is('[anchor]');mixed last() Return the last node of the current set.
$nodes = $doc->find('a'); $lastNode = $nodes->last();NodeList map(callable $function) Apply a callback to nodes in the current set and return a new NodeList.
$nodes = $doc->find('a'); $nodeValues = $nodes->map(function($node) { return $node->nodeValue; });\DOMNode|null following([string|NodeList|\DOMNode|callable $selector = null]) Return the sibling immediately following each element node in the current set.
Optionally filtered by selector.
$nodes = $doc->find('a'); $follwingNodes = $nodes->following();NodeList followingAll([string|NodeList|\DOMNode|callable $selector = null]) Return all siblings following each element node in the current set.
Optionally filtered by selector.
$nodes = $doc->find('a'); $follwingAllNodes = $nodes->followingAll('[anchor]');NodeList followingUntil([[string|NodeList|\DOMNode|callable $input = null], string|NodeList|\DOMNode|callable $selector = null]) Return all siblings following each element node in the current set upto but not including the node matched by $input.
Optionally filtered by input.
Optionally filtered by selector.
$nodes = $doc->find('a'); $follwingUntilNodes = $nodes->followingUntil('.submit');NodeList not(string|NodeList|\DOMNode|callable $input) Return element nodes from the current set not matching the input.
$nodes = $doc->find('a'); $missingHrefAttribute = $nodes->not('[href]');Element|NodeList|null parent([string|NodeList|\DOMNode|callable $selector = null]) Return the immediate parent of each element node in the current set.
Optionally filtered by selector.
$nodes = $doc->find('a'); $parentNodes = $nodes->parent();NodeList parent([string $selector = null]) Return the ancestors of each element node in the current set.
Optionally filtered by selector.
$nodes = $doc->find('a'); $ancestorDivNodes = $nodes->parents('div');NodeList parentsUntil([[string|NodeList|\DOMNode|callable $input, [string|NodeList|\DOMNode|callable $selector = null]) Return the ancestors of each element node in the current set upto but not including the node matched by $selector.
Optionally filtered by input.
Optionally filtered by selector.
$nodes = $doc->find('a'); $ancestorDivNodes = $nodes->parentsUntil('div');\DOMNode|null preceding([string|NodeList|\DOMNode|callable $selector = null]) Return the sibling immediately preceding each element node in the current set.
Optionally filtered by selector.
$nodes = $doc->find('a'); $precedingNodes = $nodes->preceding();NodeList precedingAll([string|NodeList|\DOMNode|callable $selector = null]) Return all siblings preceding each element node in the current set.
Optionally filtered by selector.
$nodes = $doc->find('a'); $precedingAllNodes = $nodes->precedingAll('[anchor]');NodeList precedingUntil([[string|NodeList|\DOMNode|callable $input = null], string|NodeList|\DOMNode|callable $selector = null]) Return all siblings preceding each element node in the current set upto but not including the node matched by $input.
Optionally filtered by input.
Optionally filtered by selector.
$nodes = $doc->find('a'); $precedingUntilNodes = $nodes->precedingUntil('.submit');NodeList siblings([[string|NodeList|\DOMNode|callable $selector = null]) Return siblings of each element node in the current set.
Optionally filtered by selector.
$nodes = $doc->find('p'); $siblings = $nodes->siblings();NodeList slice(int $start[, int $end]) Return a subset of the current set based on the start and end indexes.
$nodes = $doc->find('p'); // Return nodes 1 through to 3 as a new NodeList $slicedNodes = $nodes->slice(1, 3);int count() $nodes = $doc->find('p'); echo $nodes->count();self each(callable $function) $nodes = $doc->find('p'); $nodes->each(function($node){ echo $node->nodeName . "\n"; });PHP DOM Wrapper is licensed by Andrew Scott under the BSD 3-Clause License, see the LICENSE file for more details.
