PHP: SimpleXML->xpath() not working correctly

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.

$contents = @file_get_contents($url);

if (!$contents) { echo "Error for some reason.";
return;
}

// Parse the document
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML($contents);

$xml = simplexml_import_dom($doc);

foreach ($xml->xpath('//img') as $image) {
$images[] = (string)$image['src'];
}

[ Source ]

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