在本篇文章里小编给大家分享了关于php用xpath解析html的代码实例讲解,对此有需要的朋友们可以学习下。
实例1
$xml = simplexml_load_file('https://forums.eveonline.com');
$names = $xml->xpath("html/body/p/p/form/p/p/p/p/p[*]/p/p/table//tr/td[@class='topicViews']");
foreach($names as $name)
{
echo $name . "<br/>";
}
实例2
$url = 'http://www.baidu.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, fopen('php://stdout', 'w'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
curl_close($ch);
// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query('//*[@id="lg"]/img/@src');
foreach ($elements as $e) {
echo ($e->nodeValue);
}
以上就是相关的2个实例内容,以及相关的代码, 感谢大家对编程学习网的支持。
沃梦达教程
本文标题为:php用xpath解析html的代码实例讲解


猜你喜欢
- Laravel balde模板文件中判断数据为空方法 2023-08-30
- PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
- windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
- laravel实现按月或天或小时统计mysql数据的方法 2023-02-22
- PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
- 用nohup命令实现PHP的多进程 2023-09-02
- PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
- php微信公众号开发之秒杀 2022-11-23
- PHP中PDO事务处理操作示例 2022-10-15
- laravel通用化的CURD的实现 2023-03-17