Home > php教程 > php手册 > php下清空字符串中的HTML标签的代码

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

WBOY
Release: 2016-06-13 12:13:50
Original
771 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template