How can one effectively extract the src attribute of an img element from a specific website using PHP?

To extract the src attribute of an img element from a specific website using PHP, you can use PHP's DOMDocument class to parse the HTML content of the website and then locate the img element to retrieve its src attribute. This can be done by targeting the img tag and accessing its src attribute value.

<?php
$url = 'https://www.example.com'; // Specify the URL of the website
$html = file_get_contents($url); // Get the HTML content of the website
$doc = new DOMDocument();
$doc->loadHTML($html);

$images = $doc->getElementsByTagName('img'); // Get all img elements

foreach ($images as $image) {
    $src = $image->getAttribute('src'); // Get the src attribute value of the img element
    echo $src . "\n"; // Output the src attribute value
}
?>