I have read a lot of related information on the Internet, but PHP uses xpath to parse xml. Is there any function or class library related to PHP that can parse html? Thanks for checking a lot of related information on the Internet, but PHP uses xpath to parse xml. Does PHP have any related functions or class libraries that can parse html? Thank you.
Reply content:
I have read a lot of related information on the Internet, but PHP uses xpath to parse xml. Is there any function or class library related to PHP that can parse html? Thank you.
Use zend-dom directly, which is much more convenient!
Http://framework.zend.com/manual/2.3/en/modules/zend.dom.query.html
Do you need to introduce it?
$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);}
Similar to this