Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image src by class

I have this:

<a href="/Dealer-Catalog/ManufacturerID-3"><img class="brand-logo" src="http://www.teledynamics.com/tdresources/74c42cb2-dc7f-4548-b820-2946fbe160db.jpg" onerror="this.src='/Content/Css/Images/no_brand_logo_120_48.gif'" alt="ADTRAN"></a>

how to get img src (http://www.teledynamics.com/tdresources/74c42cb2-dc7f-4548-b820-2946fbe160db.jpg)

I try a lot of thinks this was last one:

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//class='brand-logo']/img/@src)");
echo "$src";
like image 756
Alexander Angelov Avatar asked Sep 26 '12 14:09

Alexander Angelov


2 Answers

That's not proper XPath syntax. Try

$nodes = $xpath->query("//img[@class='brand-logo']");
$src = $nodes->item(0)->getAttribute('src');

First you fetch the NODE that represents the image whose src you want, THEN you get the src attribute. Note that the ->query() call returns a DOMNodeList, not a node.

like image 106
Marc B Avatar answered Oct 18 '22 08:10

Marc B


Try like this

    <?php
    $html = '<a href="/Dealer-Catalog/ManufacturerID-3">
        <img class="brand-logo" src="http://www.teledynamics.com/tdresources/74c42cb2-dc7f-4548-b820-2946fbe160db.jpg"  alt="ADTRAN" />
        </a>';

    $xml = simplexml_load_string($html);
    echo $xml->img['src'];
    ?>
like image 38
Hearaman Avatar answered Oct 18 '22 09:10

Hearaman