Skip to content Skip to sidebar Skip to footer

Fetching All Images Src From Specific Div

Suppose, I have HTML structure like:

This is dummy text

<

Solution 1:

Change your XPath query as shown below:

// loading html content from remote url$html = file_get_contents("http://nepalpati.com/entertainment/22577/");
@$dom->loadHTML($html);  
...
$classname = 'content';
$img_sources = [];

// getting all images within div with class "content"$content = $finder->query("//div[@class='$classname']/p/img");
foreach ($contentas$img) {
    $img_sources[] = $img->getAttribute('src');
}
...
var_dump($img_sources);
// the output:array(2) {
  [0]=>
  string(68) "http://nepalpati.com/mediastorage/images/2072/Falgun/khole-selfi.jpg"
  [1]=>
  string(72) "http://nepalpati.com/mediastorage/images/2072/Falgun/khole-hot-selfi.jpg"
}

Post a Comment for "Fetching All Images Src From Specific Div"