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標籤
以上是php正規替換標籤的實作方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!