The issue with the code is that it is replacing the child nodes of each section tag one by one, which is causing the issue. Instead, we can replace the entire contents of the section tag with the new node that contains the custom string. Here's the updated code:
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
$htmlString = mb_convert_encoding($htmlString, 'HTML-ENTITIES', 'UTF-8');
$dom->loadHTML($htmlString);
$count = 0;
$keyPattern = 'ccpaRights';
foreach ($dom->getElementsByTagName('section') as $section)
{
$count = $count + 1;
$key = (string) $keyPattern.'Text'.$count;
$vueInterpolation = ' {{ $t('.'"'.$key.'"'.') }} ';
$newNode = $dom->createDocumentFragment();
$newNode->appendXML($vueInterpolation);
$section->nodeValue = '';
$section->appendChild($newNode);
}
$dom->saveHTML($dom);
$dom->save(public_path('/temp/result.html'));
In this updated code, we are creating a new document fragment for each section tag and appending the custom string to it. Then, we are replacing the entire contents of the section tag with the new node that contains the custom string. This way, all the child nodes of the section tag are replaced with the custom string.