3
我想與DOMDocument一起工作,但遇到一些問題。我有一個這樣的字符串:PHP使用DOMXPath剝離標籤並刪除節點
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>
我想要做的是,如果跨度有像ice-del
類保持內的內容,但刪除的跨度標籤。如果它有ice-ins
,請刪除整個節點。
如果它只是一個空的跨度<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();
所以,我的代碼工作刪除整個跨度節點,但我如何採取一個節點,並剝離出它的標籤,但保留其內容?
另外,我將如何刪除並清空跨度?我相信我可以使用正則表達式或替換,但我有點想用dom來做到這一點。
感謝