What are some best practices for extracting specific content from HTML using regular expressions in PHP?

When extracting specific content from HTML using regular expressions in PHP, it's important to be cautious as parsing HTML with regular expressions can be error-prone. It's generally recommended to use a DOM parser like SimpleXMLElement or DOMDocument for more reliable parsing. However, if you still want to use regular expressions, make sure to target specific elements or patterns accurately to avoid unexpected results.

$html = file_get_contents('example.html');

// Extract content between <title> tags
preg_match('/<title>(.*?)<\/title>/', $html, $matches);
$title = $matches[1];

echo $title;