HTML5DOMDocument extends the native DOMDocument library. It fixes some bugs and adds some new functionality.
composer require ivopetkov/html5-dom-document-php
- Preserves white spaces
- Preserves html entities
- Preserves void tags
- Allows inserting HTML code that moves the correct parts to their proper places (head elements are inserted in the head, body elements in the body)
- Allows querying the DOM with CSS selectors (currently avaiable: *, tagname, tagname#id, tagname.classname, #id, .classname)
HTML5DOMDocument is really easy to use - just like you should use DOMDocument.
<?php
require 'vendor/autoload.php';
$dom = new IvoPetkov\HTML5DOMDocument();
$dom->loadHTML('<!DOCTYPE html><html><body>Hello</body></html>');
echo $dom->saveHTML();
This is a list of all the new methods and properties that the library has added to the DOMDocument and the DOMElement classes.
public \DOMElement|null querySelector ( string $selector )
Returns the first document element matching the selector
Parameters
$selector
CSS query selector
Returns
The result DOMElement or null if not found
public DOMNodeList querySelectorAll ( string $selector )
Returns a list of document elements matching the selector
Parameters
$selector
CSS query selector
Returns
Returns a list of DOMElements matching the criteria
public \DOMElement createInsertTarget ( string $name )
Creates an element that will be replaced by the new body in insertHTML
Parameters
$name
The name of the insert target
Returns
A new DOMElement that must be set in the place where the new body will be inserted
public void insertHTML ( string $source [, string $target = 'beforeBodyEnd' ] )
Inserts a HTML document into the current document. The elements from the head and the body will be moved to their proper locations.
Parameters
$source
The HTML code to be inserted
$target
Body target position. Available values: afterBodyBegin, beforeBodyEnd or insertTarget name.
Returns
No value is returned.
public string $innerHTML
The HTML code inside the element
public string $outerHTML
The HTML code for the element including the code inside
public array getAttributes ( void )
Returns an array containing all attributes
Returns
An associative array containing all attributes
public string __toString ( void )
Returns the element outerHTML
Returns
The element outerHTML
public \DOMElement|null querySelector ( string $selector )
Returns the first child element matching the selector
Parameters
$selector
CSS query selector
Returns
The result DOMElement or null if not found
public DOMNodeList querySelectorAll ( string $selector )
Returns a list of children elements matching the selector
Parameters
$selector
CSS query selector
Returns
Returns a list of DOMElements matching the criteria
Querying the document with CSS selectors and getting the innerHTML and the outerHTML of the elements.
$dom = new IvoPetkov\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;
// Hello
echo $dom->querySelector('.content')->outerHTML;
// <div class="content">This is some text</div><
Inserting HTML code into other HTML code.
$dom = new IvoPetkov\HTML5DOMDocument();
$dom->loadHTML('
<!DOCTYPE html>
<html>
<head>
<style>...</style>
</head>
<body>
<h1>Hello</h1>
</body>
</html>
');
$dom->insertHTML('
<html>
<head>
<script>...</script>
</head>
<body>
<div>This is some text</div>
</body>
</html>
');
echo $dom->saveHTML();
// <!DOCTYPE html>
// <html>
// <head>
// <style>...</style>
// <script>...</script>
// </head>
// <body>
// <h1>Hello</h1>
// <div>This is some text</div>
// </body>
// </html>
HTML5DOMDocument is open-sourced software. It's free to use under the MIT license. See the license file for more information.
This library is created by Ivo Petkov. Feel free to contact me at @IvoPetkovCom or ivopetkov.com.