1.把html元素全部去掉,或者保留某几个html标签
代码如下:
结果为(去掉了注释):
Test paragraph. Other text2.相反,只去掉某一个html标签Test paragraph.
代码如下:
PHP
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;
}
原文链接:https://www.f2er.com/php/25411.htmlfunction 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;
}
$str = ' text';
$tags = 'font';
$a = strip_only($str,$tags); // red text
$b = strip_only($str,true); // text
?>