本文介绍了带状外部元素,即.用PHP解析器删除父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用解析器将某些span元素更改为相应的标题元素.我有以下代码:

I am using a parser to change certain span elements to corresponding heading 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();

一切正常,但是跨度也用p标签包裹起来,例如.

It all works fine, but the spans are also wrapped in p tags, eg.

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

我要删除哪个.我猜想在'appendChild'行之后,我想要一行'removeParent',但是我无法使其正常工作.有什么想法吗?

Which I want removed. I guess after the 'appendChild' line, I want a line to 'removeParent', but I can't get it to work. Any ideas?

谢谢.

推荐答案

不应$ span-> parentNode-> parentNode-> replaceChild($ h1,$ span-> parentNode);做你想要的吗?

Shouldn't $span->parentNode->parentNode->replaceChild($h1, $span->parentNode); do what you want?

要在给定节点之后添加节点,可以使用类似(未经测试!)的方法:

In order 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'));
}

这篇关于带状外部元素,即.用PHP解析器删除父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:32