neochief / html5-dom-document-php
HTML5 DOMDocument PHP库(扩展DOMDocument)
v1.1.0
2018-10-31 07:14 UTC
Requires
- php: >=7.0.0
- ext-dom: *
Requires (Dev)
- ivopetkov/docs-generator: 0.1.*
- phpunit/phpunit: 6.*
- dev-master
- 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-24 07:19:12 UTC
README
HTML5DOMDocument扩展了原生的DOMDocument库。它修复了一些错误并添加了一些新功能。
为什么使用?
- 保留HTML实体(DOMDocument不保留)
- 保留空元素标签(DOMDocument不保留)
- 允许插入HTML代码,并将其正确移动到相应位置(head元素插入到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:1.*
文档
完整文档作为此存储库的一部分可用。
示例
像使用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>
操作元素的class属性值
$dom = new IvoPetkov\HTML5DOMDocument(); $dom->loadHTML('<div class="class1"></div>'); echo $dom->querySelector('div')->classList->add('class2');
许可证
本项目采用MIT许可证。有关更多信息,请参阅许可证文件。
贡献
欢迎您提出新问题并为项目做出贡献。让我们一起使它变得出色,并以积极的方式去做。
作者
此库由Ivo Petkov(ivopetkov.com)和一些出色的人创建和维护。