您现在的位置是:网站首页> 编程资料编程资料
php用xpath解析html的代码实例讲解_php实例_
2023-05-25
233人已围观
简介 php用xpath解析html的代码实例讲解_php实例_
实例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 . "
"; }实例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内部实现打乱字符串顺序函数str_shuffle的方法_php技巧_
- PHP安装memcache扩展的步骤讲解_php技巧_
- PHP配置ZendOpcache插件加速_php技巧_
- PHP convert_uudecode()函数讲解_php基础_
- PHP安装BCMath扩展的方法_php技巧_
- php面试中关于面向对象的相关问题_php实例_
- PHP中检查isset()和!empty()函数的必要性_php技巧_
- PHP实现PDO操作mysql存储过程示例_php技巧_
- 在PHP中输出JS语句以及乱码问题的解决方案_php技巧_
- PHP实现通过文本文件统计页面访问量功能示例_php技巧_
