Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
H

html5-dom-document-php

> 前端框架
Open source

A better HTML5 parser for PHP.

603 stars0 likes1 views
WebsiteGitHub

About

A better HTML5 parser for PHP.

HTML5DOMDocument

HTML5DOMDocument extends the native DOMDocument library. It fixes some bugs and adds some new functionality.

Why use?

  • Preserves html entities (DOMDocument does not)
  • Preserves void tags (DOMDocument does not)
  • 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 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.

Install via Composer

bash
composer require "ivopetkov/html5-dom-document-php:2.*"

Documentation

Full documentation is available as part of this repository.

Examples

Use just like you should use DOMDocument:

php
loadHTML('Hello');
echo $dom->saveHTML();

Query the document with CSS selectors and get the innerHTML and the outerHTML of the elements:

php
$dom = new IvoPetkov\HTML5DOMDocument();
$dom->loadHTML('
Hello

');

echo $dom->querySelector('h1')->innerHTML;
// Hello

echo $dom->querySelector('.content')->outerHTML;
// 

Insert HTML code into a HTML document (other HTML code):

php
$dom = new IvoPetkov\HTML5DOMDocument();
$dom->loadHTML('
    
    
        
            

        
        
            
Hello

        
    
');

$dom->insertHTML('
    
        
            

        
        
            

        
    
');

echo $dom->saveHTML();
// 
//     
//         
//             

//             

//         
//         
//             
Hello

//             

//         
//     

Manipulate the values of the class attribute of an element:

php
$dom = new IvoPetkov\HTML5DOMDocument();
$dom->loadHTML('
');

echo $dom->querySelector('div')->classList->add('class2');

License

This project is licensed under the MIT License. See the license file for more information.

Contributing

Feel free to open new issues and contribute to the project. Let's make it awesome and let's do in a positive way.

Authors

This library is created and maintained by Ivo Petkov (ivopetkov.com) and some awesome folks.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPclasslistdomdomdocumenthtml

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架