编程语言
首页 > 编程语言> > PHP使用DOMXPath剥离标签并删除节点

PHP使用DOMXPath剥离标签并删除节点

作者:互联网

我正在尝试使用DOMDocument,但是遇到一些问题.我有一个像这样的字符串:

Some Content to keep
<span class="ice-cts-1 ice-del" data-changedata="" data-cid="5" data-time="1414514760583" data-userid="1" data-username="Site Administrator" undefined="Site Administrator">
     This content should remain, but span around it should be stripped
</span> 
     Keep this content too
<span>
     <span class="ice-cts-1 ice-ins" data-changedata="" data-cid="2" data-time="1414512278297" data-userid="1" data-username="Site Administrator" undefined="Site Administrator">
         This whole node should be deleted
     </span>
</span>

我想做的是,如果span具有像ice-del这样的类,则保留内部内容,但删除span标签.如果结冰,请删除整个节点.

如果只是一个空跨度,则< span< / span>也将其删除.这是我的代码:

//this get the above mentioned string
$getVal = $array['body'][0][$a];
$dom = new DOMDocument;
$dom->loadHTML($getVal );
$xPath = new DOMXPath($dom);
$delNodes = $xPath->query('//span[@class="ice-cts-1 ice-del"]');
$insNodes = $xPath->query('//span[@class="ice-cts-1 ice-ins"]');

foreach($insNodes as $span){
    //reject these changes, so remove whole node
    $span->parentNode->removeChild($span);
}

foreach($delNodes as $span){
    //accept these changes, so just strip out the tags but keep the content
}

$newString = $dom->saveHTML();

因此,我的代码可以删除整个span节点,但是如何获取一个节点并去除其标签却保留其内容?

另外,我将如何删除并清空跨度?我敢肯定我可以使用正则表达式或替换来执行此操作,但是我有点想使用dom来执行此操作.

谢谢

解决方法:

不,我不建议使用正则表达式,我强烈建议您使用此漂亮的HTML解析器在您现在拥有的基础上构建.在这种情况下,您可以使用-> replaceChild:

$dom = new DOMDocument;
$dom->loadHTML($getVal);
$xPath = new DOMXPath($dom);

$spans = $xPath->query('//span');
foreach ($spans as $span) {
    $class = $xPath->evaluate('string(./@class)', $span);
    if(strpos($class, 'ice-ins') !== false || $class == '') {
        $span->parentNode->removeChild($span);
    } elseif(strpos($class, 'ice-del') !== false) {
        $span->parentNode->replaceChild(new DOMText($span->nodeValue), $span);
    }
}

$newString = $dom->saveHTML();

标签:xpath,domdocument,html,php
来源: https://codeday.me/bug/20191029/1956655.html