How to get src image by class
I have it:
<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 think a lot that this is the last:
$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";
+5
4 answers
This is not the correct XPath syntax. Try
$nodes = $xpath->query("//img[@class='brand-logo']");
$src = $nodes->item(0)->getAttribute('src');
First you get a NODE that represents the image whose src you want, THEN you get the src attribute. Note that calling → query () returns a DOMNodeList, not a node.
+6
<?php
$doc=new DOMDocument();
$doc->loadHTML('<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_import_dom($doc); // just to make xpath more simple
$images=$xml->xpath('//img');
foreach ($images as $img) {
echo $img['src'];
}?>
+1