php正则表达式替换标签

1.strip_tags(剥去字符串中的 HTML 标签)

strip_tags() 函数剥去字符串中的 HTML、XML 以及 PHP 的标签。

2.字符串替换

str_replace(array(“ ”,” ”,”\t”,”\r\n”,”\r”,”\n”),array(“”,”“,”“,”“,”“,”“)

3.利用正则表达式(效率稍微低一些)

s t r = p r e g r e p l a c e ( " / \s + / " , "" , str); //过滤多余回车
s t r = p r e g r e p l a c e ( " / < [ ] + / s i " , "<" , str); //过滤<__(“<”号后面带空格)

s t r = p r e g r e p l a c e ( " / < . ? > / s i " , "" , str); //注释
s t r = p r e g r e p l a c e ( " / < ( . ? ) > / s i " , "" , str); //过滤DOCTYPE
s t r = p r e g r e p l a c e ( " / < ( \/ ? h t m l . ? ) > / s i " , "" , str); //过滤html标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? h e a d . ? ) > / s i " , "" , str); //过滤head标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? m e t a . ? ) > / s i " , "" , str); //过滤meta标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? b o d y . ? ) > / s i " , "" , str); //过滤body标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? l i n k . ? ) > / s i " , "" , str); //过滤link标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? f o r m . ? ) > / s i " , "" , str); //过滤form标签
s t r = p r e g r e p l a c e ( " / c o o k i e / s i " , " C O O K I E " , str); //过滤COOKIE标签

s t r = p r e g r e p l a c e ( " / < ( a p p l e t . ? ) > ( . ? ) < ( \/ a p p l e t . ? ) > / s i " , "" , str); //过滤applet标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? a p p l e t . ? ) > / s i " , "" , str); //过滤applet标签

s t r = p r e g r e p l a c e ( " / < ( s t y l e . ? ) > ( . ? ) < ( \/ s t y l e . ? ) > / s i " , "" , str); //过滤style标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? s t y l e . ? ) > / s i " , "" , str); //过滤style标签

s t r = p r e g r e p l a c e ( " / < ( t i t l e . ? ) > ( . ? ) < ( \/ t i t l e . ? ) > / s i " , "" , str); //过滤title标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? t i t l e . ? ) > / s i " , "" , str); //过滤title标签

s t r = p r e g r e p l a c e ( " / < ( o b j e c t . ? ) > ( . ? ) < ( \/ o b j e c t . ? ) > / s i " , "" , str); //过滤object标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? o b j e c . ? ) > / s i " , "" , str); //过滤object标签

s t r = p r e g r e p l a c e ( " / < ( n o f r a m e s . ? ) > ( . ? ) < ( \/ n o f r a m e s . ? ) > / s i " , "" , str); //过滤noframes标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? n o f r a m e s . ? ) > / s i " , "" , str); //过滤noframes标签

s t r = p r e g r e p l a c e ( " / < ( i ? f r a m e . ? ) > ( . ? ) < ( \/ i ? f r a m e . ? ) > / s i " , "" , str); //过滤frame标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? i ? f r a m e . ? ) > / s i " , "" , str); //过滤frame标签

s t r = p r e g r e p l a c e ( " / < ( s c r i p t . ? ) > ( . ? ) < ( \/ s c r i p t . ? ) > / s i " , "" , str); //过滤script标签
s t r = p r e g r e p l a c e ( " / < ( \/ ? s c r i p t . ? ) > / s i " , "" , str); //过滤script标签
s t r = p r e g r e p l a c e ( " / j a v a s c r i p t / s i " , " J a v a s c r i p t " , str); //过滤script标签
s t r = p r e g r e p l a c e ( " / v b s c r i p t / s i " , " V b s c r i p t " , str); //过滤script标签
s t r = p r e g r e p l a c e ( " / o n ( [ a z ] + ) \s = / s i " , " O n 1 =" , str); //过滤script标签
str=preg_replace("/&#/si","&#", str); //过滤script标签,如javAsCript:alert(
s t r = p r e g r e p l a c e ( " / < ( \/ ? . ? ) > / s i " , "" , str);//匹配任意html标签方式

转载来自https://www.cnblogs.com/jinrenming/p/5128082.html
t h i s > c o n t e n t = p r e g r e p l a c e ( " / < ( \/ ? . ? ) > / s i " , "" , this->content);//过滤大多数的html标签

猜你喜欢

转载自blog.csdn.net/qq_27905477/article/details/80541839
0条评论
添加一条新回复