How can PHP be used to extract a portion of HTML from a webpage?

To extract a portion of HTML from a webpage using PHP, you can use the file_get_contents() function to retrieve the webpage's content, and then use functions like strpos() and substr() to extract the desired portion based on specific HTML tags or elements.

$url = 'https://www.example.com';
$html = file_get_contents($url);

$start = strpos($html, '<div class="content">');
$end = strpos($html, '</div>', $start) + 6;

$extracted_html = substr($html, $start, $end - $start);

echo $extracted_html;