PHP code example of softcreatr / html5-dom-document-php

1. Go to this page and download the library: Download softcreatr/html5-dom-document-php 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/ */

    

softcreatr / html5-dom-document-php example snippets



 = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<!DOCTYPE html><html><body>Hello World!</body></html>');
echo $dom->saveHTML();

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<!DOCTYPE html><html><body><h1>Hello</h1><div class="content">This is some text</div></body></html>');

echo $dom->querySelector('h1')->innerHTML;  // Outputs: Hello
echo $dom->querySelector('.content')->outerHTML;  // Outputs: <div class="content">This is some text</div>

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('
    <!DOCTYPE html>
    <html>
        <head><style>body { color: red; }</style></head>
        <body><h1>Hello</h1></body>
    </html>
');

$dom->insertHTML('
    <html>
        <head><script>alert("JS Script")</script></head>
        <body><div>This is some text</div></body>
    </html>
');

echo $dom->saveHTML();
// Properly merges new elements into the existing head and body.

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<div class="class1"></div>');

$div = $dom->querySelector('div');
$div->classList->add('class2');
$div->classList->remove('class1');

echo $div->getAttribute('class');  // Outputs: "class2"

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<html><body><div id="main"></div></body></html>');

$mainDiv = $dom->querySelector('#main');
$mainDiv->appendChild($dom->createInsertTarget('name1'));

$dom->insertHTML('<div id="new-content">New content</div>', 'name1');
echo $dom->saveHTML();
shell
composer