PHP code example of scotteh / php-dom-wrapper

1. Go to this page and download the library: Download scotteh/php-dom-wrapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

scotteh / php-dom-wrapper example snippets


$doc = (new Document())->html('<p>first paragraph</p><p>second paragraph</p>');
$doc->find('p')->addClass('text-center');

composer 
 php
 php
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->appendWith('<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 follow(string|NodeList|\DOMNode|callable $input)
 php
$doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>');
$doc->find('li')->first()->follow('<li>first-and-a-half</li>');


self appendWith(string|NodeList|\DOMNode|callable $input)
 php
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->find('div')->appendWith('<strong> Appended!</strong>');
 php
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->create('<strong> Appended!</strong>')->appendTo('div');

self|string attr(string $name[, mixed $value = null])
 php
$doc = (new Document())->html('<div class="text-center"></div>');
echo $doc->attr('text-center');

self precede(string|NodeList|\DOMNode|callable $input)
 php
$doc = (new Document())->html('<ul><li>first</li><li>second</li></ul>');
doc->find('li')->first()->precede('<li>zeroth</li>');
 php
$doc = (new Document())->html('<ul><li>Item</li></ul>');
$doc->find('div')->clone()->appendTo('ul'); 
 php
$doc = (new Document())->html('<ul><li class="first"></li><li class="second"></li></ul>');
$doc->find('.first')->destroy();
 php
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->find('div')->empty(); 
 php
$doc = (new Document())->html('<div class="text-center"></div>');
echo $doc->first('div')->hasClass('text-center');

string|self html([string|NodeList|\DOMNode|callable $input = null])
 php
$doc = (new Document());
$doc->html('<div class="example"></div>');
 php
$doc = (new Document())->html('<div class="example"></div>');
$doc->find('div')->appendWith('<span>Example!</span>');
echo $doc->html();

self prependWith(string|NodeList|\DOMNode|callable $input)
 php
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->find('div')->prependWith('<strong>Prepended! </strong>');
 php
$doc = (new Document())->html('<div>The quick brown fox jumps over the lazy dog</div>');
$doc->create('<strong>Prepended! </strong>')->appendTo('div');

self substituteWith(string|NodeList|\DOMNode|callable $input)
 php

string|self text([string|NodeList|\DOMNode|callable $input = null])
 php

self wrap(string|NodeList|\DOMNode|callable $input)
 php
$doc = (new Document())->html('<span>foo<span><span>bar</span>');
$doc->find->('span')->wrap('<div><p/></div>');

self wrapAll(string|NodeList|\DOMNode|callable $input)
 php
$doc = (new Document())->html('<span>foo<span><span>bar</span>');
$doc->find->('span')->wrapAll('<div><p/></div>');

self wrapInner(string|NodeList|\DOMNode|callable $input)
 php
$doc = (new Document())->html('<span>foo<span><span>bar</span>');
$doc->find('span')->wrapInner('<b><i/></b>');

NodeList add(string|NodeList|\DOMNode $input)

Element|NodeList|null closest(string|NodeList|\DOMNode|callable $input)

NodeList contents()
 php
$nodes = $doc->find('p');
$contents = $nodes->contents();

NodeList filter(string|NodeList|\DOMNode|callable $input)
 php
$nodes = $doc->filter('a')
$exampleATags = $nodes->filter('[href*=https://example.org/]');
 php
$nodes = $doc->find('a');
 php
$nodes = $doc->find('a');
$firstNode = $nodes->first();

NodeList has(string|NodeList|\DOMNode|callable $input)
 php
$nodes = $doc->find('a');
$lastNode = $nodes->last();
 php
$nodes = $doc->find('a');
$nodeValues = $nodes->map(function($node) {
    return $node->nodeValue;
});

\DOMNode|null following([string|NodeList|\DOMNode|callable $selector = null])
 php
$nodes = $doc->find('a');
$follwingNodes = $nodes->following();

NodeList followingAll([string|NodeList|\DOMNode|callable $selector = null])

NodeList followingUntil([[string|NodeList|\DOMNode|callable $input = null], string|NodeList|\DOMNode|callable $selector = null])
 php
$nodes = $doc->find('a');
$follwingUntilNodes = $nodes->followingUntil('.submit');