PHP: SimpleXML->xpath() not working correctly

0 Comments

It has to do with the namespace and document types not being set properly, so xpath() doesn't know how to parse it.

If you're only after a quick and dirty way of parsing (X)HTML, then all you need is to load it into a DOMDocument to create the SimpleXML object.

01.$contents = @file_get_contents($url);
02. 
03.if (!$contents) {   echo "Error for some reason.";
04.  return;
05.}
06. 
07.// Parse the document
08.$doc = new DOMDocument();
09.$doc->strictErrorChecking = FALSE;
10.$doc->loadHTML($contents);
11. 
12.$xml = simplexml_import_dom($doc);
13. 
14.foreach ($xml->xpath('//img') as $image) {
15.  $images[] = (string)$image['src'];
16.}

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog