ivopetkov / html5-dom-document-php
HTML5 DOMDocument PHP 库(扩展 DOMDocument)
v2.7.0
2023-11-24 17:25 UTC
Requires
- php: 7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*
- ext-dom: *
Requires (Dev)
- dev-master
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.9
- v2.2.8
- v2.2.7
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 1.1.x-dev
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.6.1
- v0.6.0
- v0.5.10
- v0.5.9
- v0.5.8
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.16
- v0.4.15
- v0.4.14
- v0.4.13
- v0.4.12
- v0.4.11
- v0.4.10
- v0.4.9
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.2
- v0.2.1
- v0.2
- v0.1
This package is auto-updated.
Last update: 2024-09-23 16:00:23 UTC
README
HTML5DOMDocument 扩展了原生 DOMDocument 库。它修复了一些错误并添加了一些新功能。
为什么使用?
- 保留 HTML 实体(DOMDocument 不保留)
- 保留空元素标签(DOMDocument 不保留)
- 允许插入 HTML 代码,将正确的部分移动到它们正确的位置(头元素插入到 head 中,body 元素插入到 body 中)
- 允许使用 CSS 选择器查询 DOM(目前可用:*, 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 和 p ~ ul。)
- 添加对 element->classList 的支持。
- 添加对 element->innerHTML 的支持。
- 添加对 element->outerHTML 的支持。
通过 Composer 安装
composer require "ivopetkov/html5-dom-document-php:2.*"
文档
完整的 文档 作为此存储库的一部分提供。
示例
像使用 DOMDocument 一样使用它
<?php require 'vendor/autoload.php'; $dom = new IvoPetkov\HTML5DOMDocument(); $dom->loadHTML('<!DOCTYPE html><html><body>Hello</body></html>'); echo $dom->saveHTML();
使用 CSS 选择器查询文档,并获取元素的 innerHTML 和 outerHTML
$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>
将 HTML 代码插入到 HTML 文档中(其他 HTML 代码)
$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>
操作元素类属性的值
$dom = new IvoPetkov\HTML5DOMDocument(); $dom->loadHTML('<div class="class1"></div>'); echo $dom->querySelector('div')->classList->add('class2');
许可证
此项目采用 MIT 许可证。有关更多信息,请参阅 许可证文件。
贡献
请随意打开新问题并为项目做出贡献。让我们使其变得出色,并以积极的方式去做。
作者
此库由 Ivo Petkov (ivopetkov.com) 和一些 出色的人 创建和维护。