PHP strip_tags保留多个HTML标签的方法
本文介绍了PHP strip_tags函数保留多个HTML标签的方法,可以使用第二个参数来设置不需要删除的标签,主要涉及到strip_tags的第二个参数
strip_tags 函数
语法
string strip_tags ( string str [, string allowable_tags] )
返回一个去除了HTML标签的字符串;可以使用第二个参数来设置不需要删除的标签。
使用方法:
前提:假如现在有这样一个字符串,
$str = "
我来自phpstudy
";1,不保留任何HTML标签,代码会是这样:
echo strip_tags($str);
// 输出:我来自phpstudy
2,只保留一个标签的话,只需要将字符串写到strip_tags的第二个参数中:
echo strip_tags($str, "");
// 输出:我来自phpstudy
3,要保留
与…多个标签,只需要将多个标签用空格分隔后写到strip_tags的第二个参数中:
echo strip_tags($str, "
");
// 输出:
我来自phpstudy
如果要使用php删除html标记中的特定标签呢?
这个就需要代码来实现了,如下:
function strip_selected_tags($text, $tags = array()) {
$args = func_get_args();
$text = array_shift($args);
$tags = func_num_args() > 2 ? array_diff($args, array($text)) : (array) $tags;
foreach($tags as $tag) {
if (preg_match_all('/
'[^>]*>([^
'>/iu', $text, $found)) {
$text = str_replace($found[0], $found[1], $text);
}
}
return preg_replace('/(
')( | |.)*/>)/iu', '', $text);
}
$str = "[url="] 123[/url]";
echo strip_selected_tags($str, array('b'));
相关阅读:
实现动画效果核心方式的js代码
Java多线程编程之Lock用法实例
Android Socket通信详解
C#实现矩阵转置的方法
AngularJS HTML编译器介绍
使用Discuz关键词服务器实现PHP中文分词
WIN7系统不能使用1394采集卡该怎么办?
C语言创建链表错误之通过指针参数申请动态内存实例分析
mysql实现本地keyvalue数据库缓存示例
mysql实现根据多个字段查找和置顶功能
Android自定义SwipeRefreshLayout高仿微信朋友圈下拉刷新
Android开发中Eclipse报错及对应处理方法总结
div嵌套 img 空白解决方法
PHP文件缓存smarty模板应用实例分析