What are potential pitfalls when using strpos and substr functions for parsing HTML in PHP?

When using strpos and substr functions for parsing HTML in PHP, potential pitfalls include the lack of robustness when dealing with complex HTML structures and the possibility of encountering unexpected behavior due to variations in HTML formatting. To address these issues, consider using PHP libraries like DOMDocument or Simple HTML DOM Parser, which provide more reliable and flexible ways to parse HTML.

// Using DOMDocument to parse HTML
$html = '<div><p>Hello, World!</p></div>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$paragraph = $dom->getElementsByTagName('p')->item(0)->nodeValue;
echo $paragraph;