极其简便的PHP HTML DOM解析器PHP Simple HTML DOM Parser/有中文手册
极其简便的PHP HTML DOM解析器PHP Simple HTML DOM Parser,有中文手册,对于需要分析HTML代码dom结构的php开发者来说,是一个极其有用的函数库,使用Jquery风格的dom节点查找语法,强烈推荐。
下面是其“快速入门”,从中文手册里节选而来//从一个URL或者文件创建一个DOM对象
$html =file_get_html(‘http://www.google.cn/’);
// 寻找所有的img标签
foreach($html->find(‘img’) as $element)
echo $element->src. ‘<br>’;
// 寻找所有的链接标签
foreach($html->find(‘a’) as $element)
echo $element->href. ‘<br>’;
// 从HTML中提取内容(不包含标签)
echofile_get_html(‘http://www.google.com/’)->plaintext;
//从字符串创建一个DOM对象
$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; // 输出: <div id=”hello”>foo</div><div id=”world”class=”bar”>World</div>
//从URL创建一个DOM对象
$html =file_get_html(‘http://slashdot.org/’);
// 寻找所有的article块
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);
更多资料下载http://sourceforge.net/projects/simplehtmldom/
中文手册http://phpdom.comsing.com/
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。