DOMDocument object enables you to find the necessary HTML objects easily.

Below sample shows how to find images efficiently

// Load the HTML text into a DOMDocument object
$html = '<html><body><img src="image1.jpg"><img src="image2.jpg"></body></html>';
$dom = new DOMDocument();
@$dom->loadHTML($html);

// Create a DOMXPath object to query the DOMDocument
$xpath = new DOMXPath($dom);

// Find all <img> elements
$images = $xpath->query('//img');

// Loop through the images and output their src attribute
foreach ($images as $image) {
    echo $image->getAttribute('src') . "<br>";
}