摘要:php正则替换标签的实现方法:首先通过“strip_tags”函数剥去字符串中的HTML标签;然后利用正则表达式替换标签,代码语句如“pregreplace("/\\s+/","",str=pregreplace("/\\s+/",""
php正则替换标签的实现方法:首先通过“strip_tags”函数剥去字符串中的HTML标签;然后利用正则表达式替换标签,代码语句如“pregreplace("/\\s+/","",str=pregreplace("/\\s+/","")”。
推荐:《PHP视频教程》
1.strip_tags(剥去字符串中的 HTML 标签)
strip_tags() 函数剥去字符串中的 HTML、XML 以及 PHP 的标签。
2.字符串替换
str_replace(array(“ ”,” ”,”\\t”,”\\r\\n”,”\\r”,”\\n”),array(“”,”“,”“,”“,”“,”“)
3.利用正则表达式(效率稍微低一些)
str=pregreplace("/\\s+/","",str); //过滤多余回车
str=pregreplace("/<[]+/si","<",str); //过滤<__(“<”号后面带空格)
str=pregreplace("/<−−.∗?−−>/si","",str); //注释
str=pregreplace("/<(.∗?)>/si","",str); //过滤DOCTYPE
str=pregreplace("/<(\\/?html.∗?)>/si","",str); //过滤html标签
str=pregreplace("/<(\\/?head.∗?)>/si","",str); //过滤head标签
str=pregreplace("/<(\\/?meta.∗?)>/si","",str); //过滤meta标签
str=pregreplace("/<(\\/?body.∗?)>/si","",str); //过滤body标签
str=pregreplace("/<(\\/?link.∗?)>/si","",str); //过滤link标签
str=pregreplace("/<(\\/?form.∗?)>/si","",str); //过滤form标签
str=pregreplace("/cookie/si","COOKIE",str); //过滤COOKIE标签
str=pregreplace("/<(applet.∗?)>(.∗?)<(\\/applet.∗?)>/si","",str); //过滤applet标签
str=pregreplace("/<(\\/?applet.∗?)>/si","",str); //过滤applet标签
str=pregreplace("/<(style.∗?)>(.∗?)<(\\/style.∗?)>/si","",str); //过滤style标签
str=pregreplace("/<(\\/?style.∗?)>/si","",str); //过滤style标签
str=pregreplace("/<(title.∗?)>(.∗?)<(\\/title.∗?)>/si","",str); //过滤title标签
str=pregreplace("/<(\\/?title.∗?)>/si","",str); //过滤title标签
str=pregreplace("/<(object.∗?)>(.∗?)<(\\/object.∗?)>/si","",str); //过滤object标签
str=pregreplace("/<(\\/?objec.∗?)>/si","",str); //过滤object标签
str=pregreplace("/<(noframes.∗?)>(.∗?)<(\\/noframes.∗?)>/si","",str); //过滤noframes标签
str=pregreplace("/<(\\/?noframes.∗?)>/si","",str); //过滤noframes标签
str=pregreplace("/<(i?frame.∗?)>(.∗?)<(\\/i?frame.∗?)>/si","",str); //过滤frame标签
str=pregreplace("/<(\\/?i?frame.∗?)>/si","",str); //过滤frame标签
str=pregreplace("/<(script.∗?)>(.∗?)<(\\/script.∗?)>/si","",str); //过滤script标签
str=pregreplace("/<(\\/?script.∗?)>/si","",str); //过滤script标签
str=pregreplace("/javascript/si","Javascript",str); //过滤script标签
str=pregreplace("/vbscript/si","Vbscript",str); //过滤script标签
str=pregreplace("/on([a−z]+)\\s∗=/si","On1=",str); //过滤script标签
str=preg_replace("/&#/si","&#",str); //过滤script标签,如javAsCript:alert(
str=pregreplace("/<(\\/?.∗?)>/si","",str);//匹配任意html标签方式
this−>content=pregreplace("/<(\\/?.∗?)>/si","",this->content);//过滤大多数的html标签