How can PHP be used to extract specific attributes, such as the 'src' attribute, from iFrames in HTML?

To extract specific attributes, such as the 'src' attribute, from iFrames in HTML using PHP, you can use the DOMDocument class to parse the HTML and then use XPath to query for the desired attribute. By loading the HTML content into a DOMDocument object, you can easily navigate the DOM tree and extract the necessary information.

// HTML content containing iFrames
$html = '<iframe src="https://www.example.com"></iframe>';

// Create a new DOMDocument
$dom = new DOMDocument();
$dom->loadHTML($html);

// Use XPath to query for iFrames and extract the 'src' attribute
$xpath = new DOMXPath($dom);
$iframes = $xpath->query('//iframe');

foreach ($iframes as $iframe) {
    $src = $iframe->getAttribute('src');
    echo $src;
}