栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

极其简便的PHP HTML DOM解析器

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

极其简便的PHP HTML DOM解析器

极其简便的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 . ‘
’;

// 寻找所有的链接标签
foreach($html->find(‘a’) as $element)
echo $element->href . ‘
’;

// 从HTML中提取内容(不包含标签)
echo file_get_html(‘http://www.google.com/’)->plaintext;

//从字符串创建一个DOM对象
$html = str_get_html(‘HelloWorld’);


$html->find(‘div’, 1)->class = ‘bar’;

$html->find(‘div[id=hello]’, 0)->innertext = ‘foo’;

echo $html; // 输出: fooclass=”bar”>World

//从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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/229753.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号