Strip external element i.e. remove parent php parser

I am using a parser to change some span elements to the corresponding header elements. I have the following code:

$headingReplace[1]['h']     = 'h1';
$headingReplace[1]['string']    = '/html/body//span[@class="heading1"]';            $headingReplace[1]['h']     = 'h1';

    foreach($headingReplace as $heading){
        foreach ($xp->query($heading['string']) as $span) {
            $h1 = $dom->createElement($heading['h']);
            $h1->setAttribute('class', $span->getAttribute('class'));
            while($span->childNodes->length > 0) {
                $h1->appendChild($span->childNodes->item(0));
            }
            $span->parentNode->replaceChild($h1, $span);
        }   
    }
    return $dom->saveHtml();

Everything works fine, but the gaps are also wrapped in p tags, for example.

<p><span class="heading1">Blah Blah</span></p>

Which I want to delete. I assume that after the string 'appendChild' I need the string 'removeParent', but I cannot get it to work. Any ideas?

Thanks in advance.

0
source share
1 answer

Not worth $ span-> parentNode-> parentNode-> replaceChild ($ h1, $ span-> parentNode); do what you want?

To add a node after a given node, you can use something like (untested!):

if ($h1->nextSibling) {
    $h1->parentNode->insertBefore($h1->nextSibling, $dom->createElement('br'));
} else {
    $h1->parentNode->appendChild($dom->createElement('br'));
}
0
source

All Articles