加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 批量替换html标签的实例代码

发布时间:2020-12-12 19:58:36 所属栏目:PHP教程 来源:网络整理
导读:1.把html元素全部去掉,或者保留某几个html标签 代码如下: $text = ' Test paragraph. '; echo strip_tags($text); echo "/n"; // Allow and echo strip_tags($text,' '); ?> 结果为(去掉了注释): Test paragraph. Other text Test paragraph. 2.相反,只去

1.把html元素全部去掉,或者保留某几个html标签

代码如下:
$text = '

Test paragraph.

';
echo strip_tags($text);
echo "/n";

// Allow

and
echo strip_tags($text,'

');
?>


结果为(去掉了注释):

Test paragraph. Other text

Test paragraph.

2.相反,只去掉某一个html标签
代码如下:
function strip_only($str,$tags,$stripContent = false) {
$content = '';
if(!is_array($tags)) {
$tags = (strpos($str,'>') !== false ? explode('>',str_replace('<','',$tags)) : array($tags));
if(end($tags) == '') array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
$content = '(.+]*>|)';
$str = preg_replace('#]*>'.$content.'#is',$str);
}
return $str;
}

$str = ' text';
$tags = 'font';
$a = strip_only($str,$tags); // red text
$b = strip_only($str,true); // text
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读