How can the strpos() function be utilized to efficiently extract image URLs from HTML content in PHP?

When extracting image URLs from HTML content in PHP, the strpos() function can be utilized to efficiently locate the beginning and end positions of the image URLs within the HTML. By finding the position of the image URL within the HTML content, we can then extract the URL using substr() or similar string manipulation functions.

$htmlContent = "<html><body><img src='image1.jpg'><img src='image2.jpg'></body></html>";

$startTag = "src='";
$endTag = "'";
$offset = 0;

while (($startPos = strpos($htmlContent, $startTag, $offset)) !== false) {
    $startPos += strlen($startTag);
    $endPos = strpos($htmlContent, $endTag, $startPos);
    $imageURL = substr($htmlContent, $startPos, $endPos - $startPos);
    echo $imageURL . "\n";
    $offset = $endPos;
}