0

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);

$link = $xpath->query('//script[contains(@src, "pcode")]');

$return = array();
foreach($link as $links) {
  $return[] = $links->nodeValue;
}
3
  • Without input sample this is not a real question. Besides that if those script have @src they shouldn't have content, you are using the wrong DOM method. See textContent from DOM level 3. Commented Apr 28, 2011 at 16:03
  • @Alejandro i don't want the content just the links Commented Apr 28, 2011 at 17:17
  • If by "link" you mean the src attribute, then you should be selecting them with //script/@src[contains(.,"pcode")]. Commented Apr 28, 2011 at 19:40

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.