Try iterating over the child nodes of the P
elements:
foreach ($rows as $row)
{
$paragraphs = $row->getElementsByTagName('p');
//ensure that all the text between <br> is in one text node
$paragraphs->item(0)->normalize();
foreach($paragraphs->item(0)->childNodes as $node) {
if ($node->nodeType == XML_TEXT_NODE) {
echo $node->nodeValue . '<br/>;
}
}
}
It is important to call normalize() on the p
element, to ensure that the texts between br
elements are in one text node each, and not separated, eg <p>Calories (kcal)<br>Energy (kj)<br>...</p>
will have text nodes of Calories (kcal)
and Energy (kj)
, not Cal
, ories (
, kcal)
and so on, which they might be without normalizing.