Stars: 566
Forks: 61
Pull Requests: 16
Issues: 41
Watchers: 29
Last Updated: 2023-02-05 12:43:45
A better HTML5 parser for PHP.
License: MIT License
Languages: PHP
HTML5DOMDocument extends the native DOMDocument library. It fixes some bugs and adds some new functionality.
composer require "ivopetkov/html5-dom-document-php:2.*"
Full documentation is available as part of this repository.
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();
Query the document with CSS selectors and get 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>
Insert HTML code into a HTML document (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>
Manipulate the values of the class attribute of an element:
$dom = new IvoPetkov\HTML5DOMDocument();
$dom->loadHTML('<div class="class1"></div>');
echo $dom->querySelector('div')->classList->add('class2');
This project is licensed under the MIT License. See the license 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 by Ivo Petkov (ivopetkov.com) and some awesome folks.