mistralys/simple_html_dom
最新稳定版本:2.0.0
Composer 安装命令:
composer require mistralys/simple_html_dom
包简介
A fast, forgiving HTML parser for PHP.
README 文档
README
A fast, forgiving HTML parser for PHP 8.4+ that lets you find and manipulate HTML elements using CSS-like selectors — no ext-dom required.
✨ Features
- CSS-like selectors — find elements by tag,
#id,.class,[attribute], and more - Two APIs, one engine — use procedural functions (
str_get_html,file_get_html) or the namespaced OOP interface (Parser,Node) - Forgiving parser — handles messy, real-world HTML without choking
- Read and modify — change attributes, inner/outer HTML, and serialize back to string or file
- Tree traversal — navigate parents, children, and siblings
- Callbacks — hook into serialization with per-node callback functions
- Charset conversion — automatic detection and conversion via
ext-mbstring - Configurable — adjust max file size, encoding, and parser behavior at runtime
- Fully backward compatible — legacy
simple_html_domcode works without changes
📋 Requirements
- PHP 8.4+
ext-mbstring
🚀 Quick Start
Install via Composer:
composer require shark/simple_html_dom
Note: If the package is not available on Packagist, add the repository to your
composer.json:"repositories": [{ "type": "vcs", "url": "https://github.com/Mistralys/simple_html_dom.git" }]
Parse HTML and find elements:
use SimpleHtmlDom\Parser; $html = new Parser('<ul><li class="active">One</li><li>Two</li></ul>'); // Find by CSS selector $active = $html->find('li.active', 0); echo $active->plaintext; // "One" // Modify and output $active->class = 'done'; echo $html->save(); // <ul><li class="done">One</li><li>Two</li></ul>
Or use the procedural API:
$html = str_get_html('<p>Hello <b>World</b></p>'); echo $html->find('b', 0)->plaintext; // "World"
📖 Learn More
| Resource | Description |
|---|---|
| Manual | Full documentation — 14 guides covering selectors, DOM traversal, modification, configuration, and more |
| Examples | 16 runnable PHP scripts organized by topic |
| Changelog | Version history and recent changes |
Development
composer install # Install dev dependencies composer test # Run the full test suite (PHPUnit 12.x) composer analyze # Run static analysis (PHPStan Level 6)
📜 License
🔗 Origin
Fork of samacs/simple_html_dom, originally by S.C. Chen (SourceForge).
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-07