剥离 HTML 标记及其内容

2022-01-18 00:00:00 tags dom php html strip

我正在使用 DOM 来解析字符串.我需要剥离跨度标签及其内容的功能.例如,如果我有:

这是一些包含照片的文本.<跨度类='标题'>photobyile</span>

我希望函数返回

这是一些包含照片的文本.

这是我尝试过的:

 $dom = new domDocument;$dom->loadHTML($string);$dom->preserveWhiteSpace = false;$spans = $dom->getElementsByTagName('span');foreach($spans as $span){$naslov = $span->nodeValue;回声$纳斯洛夫;$string = preg_replace("/$naslov/", " ", $string);}

我知道 $span->nodeValue 返回 span 标签的值而不是整个标签,但我不知道如何获取整个标签以及类名.p>

谢谢,伊莱

解决方案

尝试直接从 DOM 树中删除 span.

$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();

I'm using DOM to parse string. I need function that strips span tags and its contents. For example, if I have:

This is some text that contains photo.
<span class='title'> photobyile</span>

I would like function to return

This is some text that contains photo.

This is what I tried:

    $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);
    }

I'm aware that $span->nodeValue returns value of span tag and not whole tag, but I don't know how to get whole tag, together with class name.

Thanks, Ile

解决方案

Try removing the spans directly from the DOM tree.

$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();

相关文章