简体   繁体   中英

Getting element inside other element by class 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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM