I am scanning a site using a dom document and receiving the @src
attribute from the inner img
element.
The html is:
<div class="content">
<div class="post">
<img src="abc"/>
</div>
<div class="post">
<img src="abc1"/>
</div>
<div class="post">
</div>
</div>
Note: I am specifically not using @src
in my xpath
. Also, this is precisely the way i need it to be coded.
Here is my php:
$document = new DOMDocument();
$document->loadXML($file);
$XPath = new DOMXPath($document);
$Query = '//div[@class="content"]//div[@class="post"]';
$posts = $XPath->query($Query);
foreach ($posts as $post) {
if($XPath->evaluate("@src", $post))
{
$return[] = $XPath->evaluate("@src", $post)->item(0);
}else{
$return[] = "";
}
}
It's adding positions to the array $return
however they are all empty array positions.
My question is how do i make it output the data from the php code:
$return[] = $XPath->evaluate("@src", $post)->item(0);
This doesn't work:
$return[] = $XPath->evaluate("@src", $post)->item(0)->nodeValue;
.//@src[1]
:
.
=> relative to node //@
=> descendant @src
=> the src attribute [1]
=> the first one. You can even use string(nodeset)
if you only care about the value (of course, leave that out if you need to be able to manipulate the attributes and use your ->item(0)
solution).
foreach ($posts as $post){
$return[] = $XPath->evaluate("string(.//@src[1])",$post);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.