Home > Backend Development > PHP Tutorial > php下清空字符串中的HTML标签的代码_php技巧

php下清空字符串中的HTML标签的代码_php技巧

WBOY
Release: 2016-05-17 09:23:14
Original
792 people have browsed it

本文介绍了php 清空字符串中的html标签
要过滤字符串中所有的html标签有两种方法一种是我们自己写一个函数,用正则过滤,一个是用php自带函数strip_tags哦。

复制代码 代码如下:

function clear_html_label($html)
{
$search = array ("'<script>]*?>.*?</script>'si", "']*?>'si", "'([rn])[s]+'", "'&(quot|#34);'i", "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'(d+);'e");
$replace = array ("", "", "1", """, "&", "", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)");

return preg_replace($search, $replace, $html);
}

//实例应用

$string ='aaa
<script>fdsafsa'; <BR>echo clear_html_label($string);//aaa fdsafsa <br><br>//利用php自带函数strip_tags(); www.zzarea.com <BR>echo strip_tags($string);//aaa fdsafsa <BR></script>


总结,
上面二个函数得出的结果完全相同,一个是用户自定义的过滤所有html函数,一个是php内置函数,但在效绿上来说php的strip_tags()函数,肯定要高很多。至少为什么我就不说多了。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template