Question

How to get alt attribute from a specific div image by php scraper ? Just look my following code sample. I can print all img "alt" attribute. But I would like to get the "alt" attribute of a specific div class. How can? Here is my code sample:

<?php
error_reporting(E_ALL);
error_reporting(1);
set_time_limit(0);

require 'simple_html_dom.php';
$url = "mylink.com";   
$html = file_get_html($url);
foreach($html->find('img') as $element) 
echo $element->alt.'<br>';
?>
Was it helpful?

Solution

Let's say the div's class is foo:

foreach($html->find('div.foo') as $div) {
  echo $div->alt . '<br>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top