去除html标签工具有哪些方法,需要注意什么
php默认的函数有移除指定html标签,名称为strip_tags,在某些场合非常有用。
strip_tags
(php 3 > = 3.0.8, php 4, php 5)
strip_tags — strip html and php tags from a string
string strip_tags ( string str [, string allowable_tags] )
弊端 :
这个函数只能保留想要的html标签,就是参数string allowable_tags。
这个函数的参数allowable_tags的其他的用法。
strip_tags($source, ”); 去掉所以的html标签。
strip_tags($source, ‘
’); 保留字符串中的div、img、em标签。
如果想去掉的html的指定标签。那么这个函数就不能满足需求了。于是乎我用到了这个函数。
/**
* removes specific tags.
*/
function strip_only_tags($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 = ‘(.+|\s[^> ]*> )|)’;
}
$str = preg_replace(‘#|\s[^> ]*> )’.$content.’#is’, ”, $str);
}
return $str;
}
参数说明
$str — 是指需要过滤的一段字符串,比如div、p、em、img等html标签。
$tags — 是指想要移除指定的html标签,比如a、img、p等。
$stripcontent = false — 移除标签内的内容,比如将整个链接删除等,默认为false,即不删除标签内的内容。
使用说明
$target = strip_only_tags($source, array(‘a’,’em’,’b’));
移除$source字符串内的a、em、b标签。
$source='
this a example fromlixiphp!
‘;
$target = strip_only_tags($source, array(‘a’,’em’));
//target results
//
this a example from!
:left; ”
以上就是关于“去除html标签工具有哪些方法,需要注意什么”的介绍了,感谢各位的阅读,希望文本对大家有所帮助。如果想要了解更多知识,欢迎关注网络,小编每天都会为大家更新不同的知识。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 去除html标签工具有哪些方法,需要注意什么
本文地址: https://pptw.com/jishu/652085.html