Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过类php DOMDocument获取其他元素中的元素_Php_Domdocument - Fatal编程技术网

通过类php DOMDocument获取其他元素中的元素

通过类php DOMDocument获取其他元素中的元素,php,domdocument,Php,Domdocument,大家好,我有以下Html代码: <div class="post-thumbnail2"> <a href="http://example.com" title="Title"> <img src="http://linkimgexample/image.png" alt="Title"/> </a> </div> 返回,这意味着您可以通过检查length属性来确定是否找到img-元素 $imgs = $di

大家好,我有以下Html代码:

<div class="post-thumbnail2">
   <a href="http://example.com" title="Title">
       <img src="http://linkimgexample/image.png" alt="Title"/>
   </a>
</div>
返回,这意味着您可以通过检查
length
属性来确定是否找到
img
-元素

$imgs = $div->getElementsByTagName("img"); 
if($imgs->length > 0) {
    foreach ($imgs as $img)
        echo $img->getAttribute("src")."<br/>";
} else {
    echo "there is no image here!<br/>";
}
$imgs=$div->getElementsByTagName(“img”);
如果($imgs->length>0){
foreach($imgs作为$img)
echo$img->getAttribute(“src”)。“
”; }否则{ echo“这里没有图像!
”; }
您应该考虑使用-它使您在穿越DOM时更加轻松:

$doc = new DOMDocument();
if($doc->loadHtml($xmlData)) {
    $xpath = new DOMXPath($doc); 
    $postThumbLinks = $xpath->query("//div[@class='post-thumbnail2']/a");

    foreach($postThumbLinks as $link) {
        $imgList = $xpath->query("./img", $link);

        $imageLink = "there is no image here!";

        if($imgList->length > 0) {
            $imageLink = $imgList->item(0)->getAttribute('src');
        }

        echo $link->getAttribute('href'), " - ", $link->getAttribute('title'),
             " - ", $imageLink, "<br/>", PHP_EOL;
    }
} else {
    echo "can't load HTML document!", PHP_EOL;
}
$doc=newDOMDocument();
如果($doc->loadHtml($xmlData)){
$xpath=新的DOMXPath($doc);
$postThumbLinks=$xpath->query(“//div[@class='post-thumbnail2']/a”);
foreach($postThumbLinks作为$link){
$imgList=$xpath->query(“./img”,$link);
$imageLink=“这里没有图像!”;
如果($imgList->length>0){
$imageLink=$imgList->item(0)->getAttribute('src');
}
echo$link->getAttribute('href'),“-”,$link->getAttribute('title'),
“-”,$imageLink,“
”,PHP_EOL; } }否则{ echo“无法加载HTML文档!”,PHP\u EOL; }
非常感谢!当我使用XPath时,我没有得到imageLink,我仍在努力让它工作。请你解释一下这部分:$imgList=$XPath->query(“//img”,$link)之间的区别是什么;和$imgList=$xpath->query(“./img”,$link);嗯
//img
递归地返回文档根下的所有图像元素,这可能不是您想要的,因为这样会使$imageLink始终指向文档中第一个
元素的src。您可能希望使用
//img
(注意起始点处表示当前节点的点),它搜索当前节点下的img元素(通过
query
的第二个参数提供)
/img
仅返回作为a元素的直接子元素的img元素。
 <div class="post-thumbnail2">
   <a href="http://example.com" title="Title"></a>
</div>
<div class="post-thumbnail2">
       <a href="http://example1.com" title="Title">
           <img src="http://linkimgexample/image1.png" alt="Title"/>
       </a>
    </div>
<div class="post-thumbnail2">
       <a href="http://example2.com" title="Title"></a>
</div>
<div class="post-thumbnail2">
       <a href="http://example3.com" title="Title">
           <img src="http://linkimgexample/image2.png" alt="Title"/>
       </a>
</div>
http://example1.com - http://linkimgexample/image1.png
http://example2.com - there is no image here !
http://example3.com - http://linkimgexample/image2.pn
$imgs = $div->getElementsByTagName("img"); 
if($imgs->length > 0) {
    foreach ($imgs as $img)
        echo $img->getAttribute("src")."<br/>";
} else {
    echo "there is no image here!<br/>";
}
$doc = new DOMDocument();
if($doc->loadHtml($xmlData)) {
    $xpath = new DOMXPath($doc); 
    $postThumbLinks = $xpath->query("//div[@class='post-thumbnail2']/a");

    foreach($postThumbLinks as $link) {
        $imgList = $xpath->query("./img", $link);

        $imageLink = "there is no image here!";

        if($imgList->length > 0) {
            $imageLink = $imgList->item(0)->getAttribute('src');
        }

        echo $link->getAttribute('href'), " - ", $link->getAttribute('title'),
             " - ", $imageLink, "<br/>", PHP_EOL;
    }
} else {
    echo "can't load HTML document!", PHP_EOL;
}