Are there best practices for using regular expressions in PHP to extract content between specific HTML tags?

When using regular expressions in PHP to extract content between specific HTML tags, it is important to be cautious as parsing HTML with regex can be error-prone. It is recommended to use a dedicated HTML parser like DOMDocument for more reliable results. However, if you still choose to use regex, make sure to use non-greedy quantifiers and be specific in your pattern to avoid unexpected matches.

$html = '<div><p>This is some text inside a paragraph tag.</p></div>';
$pattern = '/<p>(.*?)<\/p>/s'; // Using non-greedy quantifier and s modifier for multi-line matching
preg_match($pattern, $html, $matches);
echo $matches[1]; // Output: This is some text inside a paragraph tag.