php 批量替换html标签的实例代码
1.把html元素全部去掉,或者保留某几个html标签
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "/n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
结果为(去掉了注释):
<blockquote>Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a></blockquote>2.相反,只去掉某一个html标签
<?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;
}
$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>
相关文章
ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法
ThinkPHP的数据库条件查询语句有字符串式,数组式书写方法,字符串式即是原生式而数组式的查询语句因书写方式与特定字符的原因比较复杂,今天为大家讲解一下ThinkPHP数组式查询语句的书写方法2018-09-09
最新评论