【问题标题】:Getting element inside other element by class php DOMDocument通过类php DOMDocument获取其他元素内的元素
【发布时间】:2015-03-24 19:31:49
【问题描述】:

大家好,我确实有这个 Html 代码:

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

我想使用php DOMDocument获取src图片的值(http://linkimgexample/image.png)和href链接的值(http://example.com

我为获得链接所做的事情是这样的:

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

我可以对 src img 做同样的事情

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

但有时在网站上没有图像,Html 代码是这样的:

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

所以我的问题是如何同时获得 2 值,这意味着当没有图像时我会显示一些消息

为了更清楚,这是一个例子:

<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

【问题讨论】:

    标签: php domdocument


    【解决方案1】:

    DOMElement::getElementsByTagName 返回一个DOMNodeList,这意味着您可以通过检查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/>";
    }
    

    您应该考虑使用XPath - 它让您在遍历 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;
    }
    

    【讨论】:

    • 非常感谢!当我使用 XPath 时,我没有得到 imageLink 我仍在努力让它工作你能解释一下这部分吗: $imgList = $xpath->query("//img", $link) 之间有什么区别;和 $imgList = $xpath->query("./img", $link);
    • 好吧 //img 递归返回文档根目录下的所有图像元素,这可能不是您想要的,因为这会使 $imageLink 始终指向文档中第一个 &lt;img&gt; 元素的 src .您可能想要使用.//img(注意开头的点表示当前节点),它在当前节点下搜索img 元素(通过query 的第二个参数提供)。 ./img 只返回 img 元素,它们是 a 元素的直接子元素。
    猜你喜欢
    • 2012-03-22
    • 2012-05-18
    • 2011-12-10
    • 2014-10-04
    • 2017-08-10
    • 2017-04-03
    • 2013-06-07
    • 2011-08-18
    相关资源
    最近更新 更多