php xpath parse script src
I am trying to parse all script src link values, but I get an empty array.
$dom = new DOMDocument();
$file = @$dom->loadHTML($remote);
$xpath = new DOMXpath($dom);
$li开发者_StackOverflow社区nk = $xpath->query('//script[contains(@src, "pcode")]');
$return = array();
foreach($link as $links) {
$return[] = $links->nodeValue;
}
Your XPATH query looks valid, should grab every <script>
with attribute src
containing pcode
.
If it's returning an empty array, there's a few things to check:
Make sure the DOM document and loading, and there are not errors when loading it into XPATH. It could be possible that the suppressed DOM->load is giving an error or warning. If you query elsewhere and it works, then ignore this.
Make sure the tags in your document are case-matching.
Try
$link = $xpath->query("//script[contains(@src, 'pcode')]");
Seems silly, just switching quote marks, but you never know.
Be sure to check namespaces. If your HTML contains a declaration like this
<html xmlns="http://www.w3.org/1999/xhtml">
You'll need to register the namespace with the document
$xp = new domxpath( $xml);
$xp->registerNamespace('html', 'http://www.w3.org/1999/xhtml' );
And Look for elements like this
$elements = $xp->query( "//html:script", $xml );
Namespaces, because paranoia breeds confidence.
精彩评论