I have a function to grab images src
attribute in a section
tag. but it give me images src
out of this specified section tag (selected by class
).
$doc = new DOMDocument();
@$doc->loadHTMLFile('http://www.akairan.com/health/bimari-behdasht/news201691911413397586.html');
$finder = new DomXPath($doc);
$imgNodes = $finder->query('//article[@class="contentpaneopen_text"]//img/@src');
$images = array();
foreach($imgNodes as $node) {
echo $node->nodeValue . "<br />";
}
The result is:
http://cdn64.akairan.com/files/images/20160919/20169/2016919114132436510a.jpg
http://cdn2.akairan.com/akairan/telegram.jpg
http://ser8.akairan.com/img/cdn/125/30/icmjthidoctor.ir.jpg
http://ser9.akairan.com/img/cdn/180/80/akairan__aka__m998__027343234141194102a.jpg
http://ser9.akairan.com/img/cdn/180/80/akairan__aka__m998__074496544283135102a.jpg
But it should return just first link not the others.
Since there are more than one <img>
children inside <article class="contentpaneopen_text">
, it's normal that you get more than one result.
Your xpath needs to be more precise. We can see that the one you want to match should also have a parent <a>
and should have a title
attribute.
try with:
//article[@class="contentpaneopen_text"]//a/img[@title!='']/@src