剥离 HTML 标记及其内容

Strip HTML tags and its contents(剥离 HTML 标记及其内容)
本文介绍了剥离 HTML 标记及其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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();

这篇关于剥离 HTML 标记及其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)