2 [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/scotteh/php-dom-wrapper/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/scotteh/php-dom-wrapper/?branch=master)
6 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.
10 - Andrew Scott (andrew@andrewscott.net.au)
15 - PSR-4 compatible autoloader
19 Install with [Composer](https://getcomposer.org/doc/).
22 composer require scotteh/php-dom-wrapper
27 This library requires an autoloader, if you aren't already using one you can include [Composers autoloader](https://getcomposer.org/doc/01-basic-usage.md#autoloading).
30 require 'vendor/autoload.php';
37 | Method | jQuery Equivalent *(if different)* |
38 |--------|------------------------------|
39 | [addClass()](#addClass) |
40 | [follow()](#follow) | *after()* |
41 | [appendWith()](#appendWith) | *append()* |
42 | [appendTo()](#appendTo) |
45 | [destroy()](#destroy) | *remove()* |
46 | [detach()](#detach) |
48 | [hasClass()](#hasClass) |
50 | [precede()](#precede) | *before()* |
51 | [prependWith()](#prependWith) | *prepend()* |
52 | [prependTo()](#prependTo) |
53 | [removeAttr()](#removeAttr) |
54 | [removeClass()](#removeClass) |
55 | [substituteWith()](#substituteWith) | *replaceWith()* |
57 | [unwrap()](#unwrap) |
59 | [wrapAll()](#wrapAll) |
60 | [wrapInner()](#wrapInner) |
64 | Method | jQuery Equivalent *(if different)* |
65 |--------|------------------------------|
67 | [children()](#children) |
68 | [closest()](#closest) |
69 | [contents()](#contents) |
71 | [filter()](#filter) |
78 | [following()](#following) | *next()* |
79 | [followingAll()](#followingAll) | *nextAll()* |
80 | [followingUntil()](#followingUntil) | *nextUntil()* |
82 | [parent()](#parent) |
83 | [parents()](#parents) |
84 | [parentsUntil()](#parentsUntil) |
85 | [preceding()](#preceding) | *prev()* |
86 | [precedingAll()](#precedingAll) | *prevAll()* |
87 | [precedingUntil()](#precedingUntil) | *prevUntil()* |
88 | [siblings()](#siblings) |
93 | Method | jQuery Equivalent *(if different)* |
94 |--------|------------------------------|
95 | [count()](#count) | *length* (property) |
102 use DOMWrap\Document;
104 $html = '<ul><li>First</li><li>Second</li><li>Third</li></ul>';
106 $doc = new Document();
108 $nodes = $doc->find('li');
111 var_dump($nodes->count());
113 // Append as a child node to each <li>
114 $nodes->appendWith('<b>!</b>');
116 // Returns: <html><body><ul><li>First<b>!</b></li><li>Second<b>!</b></li><li>Third<b>!</b></li></ul></body></html>
117 var_dump($doc->html());
129 self addClass(string|callable $class)
135 $doc = (new Document())->html('<p>first paragraph</p><p>second paragraph</p>');
136 $doc->find('p')->addClass('text-center');
142 <p class="text-center">first paragraph</p><p class="text-center">second paragraph</p>
150 self follow(string|NodeList|\DOMNode|callable $input)
153 Insert the argument as a sibling directly after each of the nodes operated on.
158 $doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>');
159 $doc->find('li')->first()->follow('<li>first-and-a-half</li>');
168 <li>first-and-a-half</li>
178 self appendWith(string|NodeList|\DOMNode|callable $input)
184 $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
185 $doc->find('div')->appendWith('<strong> Appended!</strong>');
191 <div>The quick brown fox jumps over the lazy dog<strong> Appended!</strong></div>
199 self appendTo(string|NodeList|\DOMNode $selector)
205 $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
206 $doc->create('<strong> Appended!</strong>')->appendTo('div');
211 <div>The quick brown fox jumps over the lazy dog<strong> Appended!</strong></div>
219 self|string attr(string $name[, mixed $value = null])
222 ##### Example #1 (Set)
225 $doc = (new Document())->html('<div class="text-center"></div>');
226 $doc->attr('class', 'text-left');
232 <div class="text-left"></div>
235 ##### Example #2 (Get)
238 $doc = (new Document())->html('<div class="text-center"></div>');
239 echo $doc->attr('text-center');
253 self precede(string|NodeList|\DOMNode|callable $input)
256 Insert the argument as a sibling just before each of the nodes operated on.
261 $doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>');
262 doc->find('li')->first()->precede('<li>zeroth</li>');
280 NodeList|\DOMNode clone()
286 $doc = (new Document())->html('<ul><li>Item</li></ul>');
287 $doc->find('div')->clone()->appendTo('ul');
293 <ul><li>Item</li><li>Item</li></ul>
301 self destroy([string $selector = null])
307 $doc = (new Document())->html('<ul><li class="first"></li><li class="second"></li></ul>');
308 $doc->find('.first')->destroy();
313 <ul><li class="second"></li></ul>
321 NodeList detach([string $selector = null])
327 $doc = (new Document())->html('<ul class="first"><li>Item</li></ul><ul class="second"></ul>');
328 $el = $doc->find('ul.first li')->detach();
329 $doc->first('ul.second').append($el);
335 <ul class="first"></ul><ul class="second"><li>Item</li></ul>
349 $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
350 $doc->find('div')->empty();
364 bool hasClass(string $class)
370 $doc = (new Document())->html('<div class="text-center"></div>');
371 echo $doc->first('div')->hasClass('text-center');
385 string|self html([string|NodeList|\DOMNode|callable $input = null])
388 ##### Example #1 (Set)
391 $doc = (new Document());
392 $doc->html('<div class="example"></div>');
398 <div class="example"></div>
401 ##### Example #1 (Get)
404 $doc = (new Document())->html('<div class="example"></div>');
405 $doc->find('div')->appendWith('<span>Example!</span>');
412 <div class="example"><span>Example!</span></div>
420 self prependWith(string|NodeList|\DOMNode|callable $input)
426 $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
427 $doc->find('div')->prependWith('<strong>Prepended! </strong>');
433 <div><strong>Prepended! </strong>The quick brown fox jumps over the lazy dog</div>
441 self prependTo(string|NodeList|\DOMNode $selector)
447 $doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
448 $doc->create('<strong>Prepended! </strong>')->appendTo('div');
453 <div><strong>Prepended! </strong>The quick brown fox jumps over the lazy dog</div>
461 self removeAttr(string $name)
467 $doc = (new Document())->html('<div class="first second"></div>');
468 $doc->find('div').removeAttr('class');
481 self removeClass(string|callable $class)
487 $doc = (new Document())->html('<div class="first second"></div>');
488 $doc->find('div').removeClass('first');
493 <div class="second"></div>
501 self substituteWith(string|NodeList|\DOMNode|callable $input)
514 string|self text([string|NodeList|\DOMNode|callable $input = null])
530 Unwrap each current node by removing its parent, replacing the parent
531 with its children (i.e. the current node and its siblings).
533 Note that each node is operated on separately, so when you call
534 `unwrap()` on a `NodeList` containing two siblings, *two* parents will
540 $doc = (new Document())->html('<div id="outer"><div id="first"/><div id="second"/></div>');
541 $doc->find('#first')->unwrap();
547 <div id="first"></div>
548 <div id="second"></div>
556 self wrap(string|NodeList|\DOMNode|callable $input)
559 Wrap the current node or nodes in the given structure.
561 The wrapping structure can be nested, but should only contain one node
562 on each level (any extra siblings are removed). The outermost node
563 replaces the node operated on, while the node operated on is put into
566 If called on a `NodeList`, each of nodes in the list will be separately
567 wrapped. When such a list contains multiple nodes, the argument to
568 wrap() cannot be a `NodeList` or `\DOMNode`, since those can be used
569 to wrap a node only once. A string or callable returning a string or a
570 unique `NodeList` or `\DomNode` every time can be used in this case.
572 When a callable is passed, it is called once for each node operated on,
573 passing that node and its index. The callable should return either a
574 string, or a unique `NodeList` or `\DOMNode` ever time it is called.
576 Note that this returns the original node like all other methods, not the
577 (new) node(s) wrapped around it.
582 $doc = (new Document())->html('<span>foo<span><span>bar</span>');
583 $doc->find->('span')->wrap('<div><p/></div>');
589 <div><p><span>foo</span></p></div>
590 <div><p><span>bar</span></p></div>
599 self wrapAll(string|NodeList|\DOMNode|callable $input)
602 Like [wrap()](#wrap), but when operating on multiple nodes, all of them
603 will be wrapped together in a single instance of the given structure,
604 rather than each of them individually.
606 Note that the wrapping structure replaces the first node operated on, so
607 if the other nodes operated on are not siblings of the first, they will
608 be moved inside the document.
613 $doc = (new Document())->html('<span>foo<span><span>bar</span>');
614 $doc->find->('span')->wrapAll('<div><p/></div>');
631 self wrapInner(string|NodeList|\DOMNode|callable $input)
634 Like [wrap()](#wrap), but rather than wrapping the nodes that are being
635 operated on, this wraps their contents.
640 $doc = (new Document())->html('<span>foo<span><span>bar</span>');
641 $doc->find('span')->wrapInner('<b><i/></b>');
647 <span><b><i>foo</i></b></span>
648 <span><b><i>bar</i></b></span>
659 NodeList add(string|NodeList|\DOMNode $input)
662 Add additional node(s) to the existing set.
667 $nodes = $doc->find('a');
668 $nodes->add($doc->find('p'));
679 Return all children of each element node in the current set.
684 $nodes = $doc->find('p');
685 $childrenOfParagraphs = $nodes->children();
693 Element|NodeList|null closest(string|NodeList|\DOMNode|callable $input)
696 Return the first element matching the supplied input by traversing up through the ancestors of each node in the current set.
701 $nodes = $doc->find('a');
702 $closestAncestors = $nodes->closest('p');
713 Return all children of each node in the current set.
718 $nodes = $doc->find('p');
719 $contents = $nodes->contents();
727 \DOMNode|null eq(int $index)
730 Return node in the current set at the specified index.
735 $nodes = $doc->find('a');
736 $nodeAtIndexOne = $nodes->eq(1);
744 NodeList filter(string|NodeList|\DOMNode|callable $input)
747 Return nodes in the current set that match the input.
752 $nodes = $doc->filter('a')
753 $exampleATags = $nodes->filter('[href*=https://example.org/]');
761 NodeList find(string $selector[, string $prefix = 'descendant::'])
764 Return the decendants of the current set filtered by the selector and optional XPath axes.
769 $nodes = $doc->find('a');
780 Return the first node of the current set.
785 $nodes = $doc->find('a');
786 $firstNode = $nodes->first();
794 NodeList has(string|NodeList|\DOMNode|callable $input)
797 Return nodes with decendants of the current set matching the input.
802 $nodes = $doc->find('a');
803 $anchorTags = $nodes->has('span');
811 bool is(string|NodeList|\DOMNode|callable $input)
814 Test if nodes from the current set match the input.
819 $nodes = $doc->find('a');
820 $isAnchor = $nodes->is('[anchor]');
831 Return the last node of the current set.
836 $nodes = $doc->find('a');
837 $lastNode = $nodes->last();
845 NodeList map(callable $function)
848 Apply a callback to nodes in the current set and return a new NodeList.
853 $nodes = $doc->find('a');
854 $nodeValues = $nodes->map(function($node) {
855 return $node->nodeValue;
864 \DOMNode|null following([string|NodeList|\DOMNode|callable $selector = null])
867 Return the sibling immediately following each element node in the current set.
869 *Optionally filtered by selector.*
874 $nodes = $doc->find('a');
875 $follwingNodes = $nodes->following();
883 NodeList followingAll([string|NodeList|\DOMNode|callable $selector = null])
886 Return all siblings following each element node in the current set.
888 *Optionally filtered by selector.*
893 $nodes = $doc->find('a');
894 $follwingAllNodes = $nodes->followingAll('[anchor]');
902 NodeList followingUntil([[string|NodeList|\DOMNode|callable $input = null], string|NodeList|\DOMNode|callable $selector = null])
905 Return all siblings following each element node in the current set upto but not including the node matched by $input.
907 *Optionally filtered by input.*<br>
908 *Optionally filtered by selector.*
913 $nodes = $doc->find('a');
914 $follwingUntilNodes = $nodes->followingUntil('.submit');
922 NodeList not(string|NodeList|\DOMNode|callable $input)
925 Return element nodes from the current set not matching the input.
930 $nodes = $doc->find('a');
931 $missingHrefAttribute = $nodes->not('[href]');
939 Element|NodeList|null parent([string|NodeList|\DOMNode|callable $selector = null])
942 Return the immediate parent of each element node in the current set.
944 *Optionally filtered by selector.*
949 $nodes = $doc->find('a');
950 $parentNodes = $nodes->parent();
958 NodeList parent([string $selector = null])
961 Return the ancestors of each element node in the current set.
963 *Optionally filtered by selector.*
968 $nodes = $doc->find('a');
969 $ancestorDivNodes = $nodes->parents('div');
977 NodeList parentsUntil([[string|NodeList|\DOMNode|callable $input, [string|NodeList|\DOMNode|callable $selector = null])
980 Return the ancestors of each element node in the current set upto but not including the node matched by $selector.
982 *Optionally filtered by input.*<br>
983 *Optionally filtered by selector.*
988 $nodes = $doc->find('a');
989 $ancestorDivNodes = $nodes->parentsUntil('div');
997 \DOMNode|null preceding([string|NodeList|\DOMNode|callable $selector = null])
1000 Return the sibling immediately preceding each element node in the current set.
1002 *Optionally filtered by selector.*
1007 $nodes = $doc->find('a');
1008 $precedingNodes = $nodes->preceding();
1016 NodeList precedingAll([string|NodeList|\DOMNode|callable $selector = null])
1019 Return all siblings preceding each element node in the current set.
1021 *Optionally filtered by selector.*
1026 $nodes = $doc->find('a');
1027 $precedingAllNodes = $nodes->precedingAll('[anchor]');
1034 NodeList precedingUntil([[string|NodeList|\DOMNode|callable $input = null], string|NodeList|\DOMNode|callable $selector = null])
1037 Return all siblings preceding each element node in the current set upto but not including the node matched by $input.
1039 *Optionally filtered by input.*<br>
1040 *Optionally filtered by selector.*
1045 $nodes = $doc->find('a');
1046 $precedingUntilNodes = $nodes->precedingUntil('.submit');
1054 NodeList siblings([[string|NodeList|\DOMNode|callable $selector = null])
1057 Return siblings of each element node in the current set.
1059 *Optionally filtered by selector.*
1064 $nodes = $doc->find('p');
1065 $siblings = $nodes->siblings();
1073 NodeList slice(int $start[, int $end])
1076 Return a subset of the current set based on the start and end indexes.
1081 $nodes = $doc->find('p');
1082 // Return nodes 1 through to 3 as a new NodeList
1083 $slicedNodes = $nodes->slice(1, 3);
1088 ### Additional Methods
1099 $nodes = $doc->find('p');
1101 echo $nodes->count();
1109 self each(callable $function)
1115 $nodes = $doc->find('p');
1117 $nodes->each(function($node){
1118 echo $node->nodeName . "\n";
1124 PHP DOM Wrapper is licensed by Andrew Scott under the BSD 3-Clause License, see the LICENSE file for more details.