php 批量替换html标签的实例代码
1.把html元素全部去掉,或者保留某几个html标签
$text = '
Test paragraph.
Other text';echo strip_tags($text);
echo "/n";
// Allow
and
echo strip_tags($text, '
');
?>
结果为(去掉了注释):
Test paragraph. Other text2.相反,只去掉某一个html标签Test paragraph.
Other text
function strip_only($str, $tags, $stripContent = false) {
$content = '';
if(!is_array($tags)) {
$tags = (strpos($str, '>') !== false ? explode('>', str_replace('
if(end($tags) == '') array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
$content = '(.+'.$tag.'[^>]*>|)';
$str = preg_replace('#?'.$tag.'[^>]*>'.$content.'#is', '', $str);
}
return $str;
}
$str = 'red text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>相关阅读:
解析Mysql备份与恢复简单总结与tee命令的使用介绍
Win10 Build 9926删除开始按钮下的磁贴应用的方法
php使用百度天气接口示例
jQuery常用数据处理方法小结
C#实现窗口之间的传值
举例讲解jQuery中可见性过滤选择器的使用
整理HTML5中支持的URL编码与字符编码
MySQL批量插入数据脚本
Jquery对象和Dom对象的区别分析
Win10正式版10586.75累积更新将很快推送
C#查找对象在ArrayList中出现位置的方法
C++线程池的简单实现方法
全面解析JavaScript里的循环方法之forEach,for-in,for-of
mysql服务性能优化—my.cnf_my.ini配置说明详解(16G内存)