我正在使用DOM来解析字符串.我需要功能来剥离标签及其内容.例如,如果我有:
This is some text that contains photo. <span class='title'> photobyile</span>
我希望函数返回
This is some text that contains photo.
这是我试过的:
$dom = new domDocument; $dom->loadHTML($string); $dom->preserveWhiteSpace = false; $spans = $dom->getElementsByTagName('span'); foreach($spans as $span) { $naslov = $span->nodeValue; echo $naslov; $string = preg_replace("/$naslov/"," ",$string); }
我知道$span-> nodeValue返回span标签的值,而不是整个标签,但我不知道如何获取整个标签,以及类名.
谢谢,
岛
尝试直接从DOM树中删除跨距.
原文链接:https://www.f2er.com/php/139413.html$dom = new DOMDocument(); $dom->loadHTML($string); $dom->preserveWhiteSpace = false; $elements = $dom->getElementsByTagName('span'); while($span = $elements->item(0)) { $span->parentNode->removeChild($span); } echo $dom->saveHTML();