me578022 / simplehtmldom
一个用PHP5+编写的HTML DOM解析器,让您能够非常容易地操作HTML!
v1.11
2016-10-01 09:34 UTC
Requires
- php: >=5.2.0
This package is not auto-updated.
Last update: 2024-09-28 20:19:59 UTC
README
一个用PHP5+编写的HTML DOM解析器,让您能够非常容易地操作HTML!
需求与功能
- 一个用PHP5+编写的HTML DOM解析器,让您能够非常容易地操作HTML!
- 需要PHP 5+。
- 支持无效HTML。
- 使用选择器在HTML页面中查找标签,就像jQuery。
- 单行提取HTML中的内容。
下载与文档
- 从Sourceforge下载最新版本。
- 阅读在线文档。
快速开始
获取HTML元素
// Create DOM from URL or file $html = file_get_html('http://www.google.com/'); // Find all images foreach($html->find('img') as $element) echo $element->src . '<br>'; // Find all links foreach($html->find('a') as $element) echo $element->href . '<br>';
修改HTML元素
// Create DOM from string $html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>'); $html->find('div', 1)->class = 'bar'; $html->find('div[id=hello]', 0)->innertext = 'foo'; echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
从HTML中提取内容
// Dump contents (without tags) from HTML echo file_get_html('http://www.google.com/')->plaintext;
抓取Slashdot!
// Create DOM from URL $html = file_get_html('http://slashdot.org/'); // Find all article blocks foreach($html->find('div.article') as $article) { $item['title'] = $article->find('div.title', 0)->plaintext; $item['intro'] = $article->find('div.intro', 0)->plaintext; $item['details'] = $article->find('div.details', 0)->plaintext; $articles[] = $item; } print_r($articles);
反馈
作者
作者:S.C. Chen (me578022@gmail.com)。原始想法来自Jose Solorzano的PHP 4 HTML解析器。贡献者:Yousuke Kumakura(属性过滤器)