Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork42
A better HTML5 parser for PHP.
License
ivopetkov/html5-dom-document-php
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
HTML5DOMDocument extends the nativeDOMDocument library. It fixes some bugs and adds some new functionality.
- Preserves html entities (DOMDocument does not)
- Preserves void tags (DOMDocument does not)
- Allowsinserting HTML code that moves the correct parts to their proper places (head elements are inserted in the head, body elements in the body)
- Allowsquerying the DOM with CSS selectors (currently available: *, tagname, tagname#id, #id, tagname.classname, .classname, tagname.classname.classname2, .classname.classname2, tagname[attribute-selector], [attribute-selector], "div, p", div p, div > p, div + p and p ~ ul.)
- Adds support for element->classList.
- Adds support for element->innerHTML.
- Adds support for element->outerHTML.
composer require"ivopetkov/html5-dom-document-php:2.*"Fulldocumentation is available as part of this repository.
Use just like you should use DOMDocument:
<?phprequire'vendor/autoload.php';$dom =newIvoPetkov\HTML5DOMDocument();$dom->loadHTML('<!DOCTYPE html><html><body>Hello</body></html>');echo$dom->saveHTML();
Query the document with CSS selectors and get the innerHTML and the outerHTML of the elements:
$dom =newIvoPetkov\HTML5DOMDocument();$dom->loadHTML('<!DOCTYPE html><html><body><h1>Hello</h1><div>This is some text</div></body></html>');echo$dom->querySelector('h1')->innerHTML;// Helloecho$dom->querySelector('.content')->outerHTML;// <div>This is some text</div>
Insert HTML code into a HTML document (other HTML code):
$dom =newIvoPetkov\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>
Manipulate the values of the class attribute of an element:
$dom =newIvoPetkov\HTML5DOMDocument();$dom->loadHTML('<div></div>');echo$dom->querySelector('div')->classList->add('class2');
This project is licensed under the MIT License. See thelicense file for more information.
Feel free to open new issues and contribute to the project. Let's make it awesome and let's do in a positive way.
This library is created and maintained byIvo Petkov (ivopetkov.com) and someawesome folks.
About
A better HTML5 parser for PHP.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.