php用xpath解析html的代碼實例講解
更新時間:2019年02月14日 14:44:34 投稿:laozhang
在本篇文章里小編給大家分享了關于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個實例內(nèi)容,以及相關的代碼, 感謝大家對腳本之家的支持。
相關文章
php array_values 返回數(shù)組的值實例詳解
php array_values 函數(shù)用于返回數(shù)組中所有的值,注意該函數(shù)將為新數(shù)組建立數(shù)組索引,原來的文字索引將不存在。本文章向大家講解array_values函數(shù)的基本語法及使用實例,需要的朋友可以參考下2016-11-11PHP如何將數(shù)據(jù)庫查詢結(jié)果輸出為json格式
JSON數(shù)據(jù)格式相比xml的解析難度較低,配合Ajax技術的使用使得前端獲取數(shù)據(jù),呈現(xiàn)數(shù)據(jù)非常方便,所以下面這篇文章主要給大家介紹了關于PHP如何將數(shù)據(jù)庫查詢結(jié)果輸出為json格式的相關資料,需要的朋友可以參考下2022-02-02