What are the common pitfalls when trying to differentiate text content between HTML tags using PHP DOM manipulation?

One common pitfall when trying to differentiate text content between HTML tags using PHP DOM manipulation is not accounting for whitespace or line breaks that may be present in the text nodes. To accurately extract and differentiate text content, it is important to trim any leading or trailing whitespace from the text nodes.

// Load the HTML content into a DOMDocument
$html = '<div><p>Text content</p></div>';
$dom = new DOMDocument();
$dom->loadHTML($html);

// Get the text content of the <p> tag without leading or trailing whitespace
$pTag = $dom->getElementsByTagName('p')->item(0);
$textContent = trim($pTag->textContent);

echo $textContent;