1.把html元素全部去掉,或者保留某几个html标签
$text = '
echo strip_tags($text);
echo "/n";
Test paragraph.
';echo strip_tags($text);
echo "/n";
// Allow
结果为(去掉了注释):
Test paragraph. Other text2.相反,只去掉某一个html标签Test paragraph.
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 = '(.+'.$tag.'[^>]*>|)';
$str = preg_replace('#?'.$tag.'[^>]*>'.$content.'#is',$str);
}
return $str;
}
$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 = '(.+'.$tag.'[^>]*>|)';
$str = preg_replace('#?'.$tag.'[^>]*>'.$content.'#is',$str);
}
return $str;
}
$str = ' text';
$tags = 'font';
$a = strip_only($str,$tags); // red text
$b = strip_only($str,true); // text
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。