dongquming3255 2015-03-24 19:31
浏览 64
已采纳

通过类php DOMDocument将元素放入其他元素中

Hi Guys i do have this Html Code :

<div class="post-thumbnail2">
   <a href="http://example.com" title="Title">
       <img src="http://linkimgexample/image.png" alt="Title"/>
   </a>
</div>

I want to get the value of src image (http://linkimgexample/image.png) and the value of the href link (http://example.com) using php DOMDocument

what i did to get the link was something like that :

$divs = $dom->getElementsByTagName("div");

    foreach($divs as $div) { 
        $cl = $div->getAttribute("class");

        if ($cl == "post-thumbnail2") {
            $links = $div->getElementsByTagName("a");
            foreach ($links as $link)
                    echo $link->getAttribute("href")."<br/>";
        }
    }

i could do the same for src img

$imgs = $div->getElementsByTagName("img"); 
foreach ($imgs as $img)
    echo $img->getAttribute("src")."<br/>";

but sometime in the website there is no image and the Html code is like that :

 <div class="post-thumbnail2">
   <a href="http://example.com" title="Title"></a>
</div>

so my questions is how could i get the 2 value at the same time it means when there is no image i show some message

to be more clear this is an example :

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

i want the result to be

http://example1.com - http://linkimgexample/image1.png
http://example2.com - there is no image here !
http://example3.com - http://linkimgexample/image2.pn
  • 写回答

1条回答 默认 最新

  • douxuan4556 2015-03-24 20:17
    关注

    DOMElement::getElementsByTagName returns a DOMNodeList, that means you can find out if a img-element was found by checking the length property.

    $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/>";
    }
    

    You should think about using XPath - it makes your life traversing the DOM a bit easier:

    $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效