在ASP版本和PHP版本都有这个函数的,只是内容有些诧异,因为PHP原生没有UBB功能,所以PHP的过滤函数里忽略了UBB功能(其实老子是需要的啊)
ASP版本的内容如下:
'********************************************************* ' 目的: 检查引用 ' 输入: ' 输入: 要替换的字符代号 ' 返回: '********************************************************* Function TransferHTML(ByVal source,ByVal para) Dim objRegExp If IsNull(source)=True Then Exit Function If InStr(para,"[mobilerequest]") Then para=para&"[enter][closehtml]" '如何判断HTML标签和用户输入的类似0<1这种数据,还真是个大麻烦 End If '先换"&" If Instr(para,"[&]")>0 Then source=Replace(source,"&","&") If Instr(para,"[<]")>0 Then source=Replace(source,"<","<") If Instr(para,"[>]")>0 Then source=Replace(source,">",">") If Instr(para,"[""]")>0 Then source=Replace(source,"""",""") If Instr(para,"[space]")>0 Then source=Replace(source," "," ") If Instr(para,"[delspace]")>0 Then Source=Replace(source," ","") Source=Replace(source," ","") End If If Instr(para,"[enter]")>0 Then source=Replace(source,vbCrLf,"<br/>") source=Replace(source,vbLf,"<br/>") End If If Instr(para,"[vbCrlf]")>0 Then Set objRegExp=New RegExp objRegExp.IgnoreCase =True objRegExp.Global=True objRegExp.Pattern="((</?form[^\n<]*>)|(<select[^\n<]*>)|(<textarea[^\n<]*>)|(</?option[^\n<]*>)|(</?dl[^\n<]*>)|(</?dt[^\n<]*>)|(</?dd[^\n<]*>)|(</?th[^\n<]*>)|(</?tr[^\n<]*>)|(</?td[^\n<]*>)|(</?tbody[^\n<]*>)|(</?table[^\n<]*>)|(</?hr[^\n<]*>)|(</?div[^\n<]*>)|(</?ul[^\n<]*>)|(</?li[^\n<]*>)|(</?ol[^\n<]*>)|(</?h1[^\n<]*>)|(</?h2[^\n<]*>)|(</?h3[^\n<]*>)|(</?h4[^\n<]*>)|(</?h5[^\n<]*>)|(</?h6[^\n<]*>)|(</?p[^\n<]*>))(\x20*(\r\n|\n))" source= objRegExp.Replace(source,"$1") objRegExp.Pattern="(\r\n|\n)" source= objRegExp.Replace(source,"<br/>") source=Replace(source,"<html>","") source=Replace(source,"</html>","") source=Replace(source,"<body>","") source=Replace(source,"</body>","") End If If Instr(para,"[vbTab]")>0 Then source=Replace(source,vbTab," ") If Instr(para,"[upload]")>0 Then source=Replace(source,"src=""upload/","src=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/") source=Replace(source,"href=""upload/","href=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/") source=Replace(source,"value=""upload/","value=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/") source=Replace(source,"href=""http://upload/","href=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/") source=Replace(source,"(this.nextSibling,'upload/","(this.nextSibling,'<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/") source=Replace(source,"src=""image/face/","src=""<#ZC_BLOG_HOST#>zb_users/emotion/face/") End If If Instr(para,"[anti-upload]")>0 Then source=Replace(source,"src="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","src=""upload/") source=Replace(source,"href="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","href=""upload/") source=Replace(source,"value="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","value=""upload/") source=Replace(source,"href="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","href=""http://upload/") source=Replace(source,"(this.nextSibling,'"& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","(this.nextSibling,'upload/") source=Replace(source,"src="""& GetCurrentHost() & "zb_users/emotion/face/","src=""<#ZC_BLOG_HOST#>zb_users/emotion/face/") End If If Instr(para,"[zc_blog_host]")>0 Then source=Replace(source,"<#ZC_BLOG_HOST#>",GetCurrentHost()) End If If Instr(para,"[no-asp]")>0 Then source=Replace(source,"<"&"%","<"&"%") source=Replace(source,"%"&">","%"&">") End If If Instr(para,"[nofollow]")>0 Then source=Replace(source,"<a ","<a rel=""nofollow"" ") End If If Instr(para,"[nohtml]")>0 Then Set objRegExp=New RegExp objRegExp.IgnoreCase =True objRegExp.Global=True objRegExp.Pattern="<[^>]*>" source= objRegExp.Replace(source,"") End If If Instr(para,"[filename]")>0 Then source=Replace(source,"/","") source=Replace(source,"\","") source=Replace(source,":","") source=Replace(source,"?","") source=Replace(source,"*","") source=Replace(source,"""","") source=Replace(source,"<","") source=Replace(source,">","") source=Replace(source,"|","") source=Replace(source," ","") End If If Instr(para,"[directory&file]")>0 Then source=Replace(source,"/","/") source=Replace(source,"\","/") source=Replace(source,":","") source=Replace(source,"?","") source=Replace(source,"*","") source=Replace(source,"""","") source=Replace(source,"<","") source=Replace(source,">","") source=Replace(source,"|","") source=Replace(source," ","") End If If Instr(para,"[normalname]")>0 Then source=Replace(source,"$","") source=Replace(source,"(","") source=Replace(source,")","") source=Replace(source,"*","") source=Replace(source,"+","") source=Replace(source,",","") source=Replace(source,"[","") source=Replace(source,"]","") source=Replace(source,"{","") source=Replace(source,"}","") source=Replace(source,"?","") source=Replace(source,"\","") source=Replace(source,"^","") source=Replace(source,"|","") source=Replace(source,":","") source=Replace(source,"""","") source=Replace(source,"'","") End If If Instr(para,"[normaltag]")>0 Then source=Replace(source,"$","") source=Replace(source,"(","") source=Replace(source,")","") source=Replace(source,"*","") source=Replace(source,"+","") source=Replace(source,"[","") source=Replace(source,"]","") source=Replace(source,"{","") source=Replace(source,"}","") source=Replace(source,"?","") source=Replace(source,"\","") source=Replace(source,"^","") source=Replace(source,"|","") source=Replace(source,":","") source=Replace(source,"""","") source=Replace(source,"'","") Set objRegExp=New RegExp objRegExp.IgnoreCase =True objRegExp.Global=True objRegExp.Pattern=",+" source= objRegExp.Replace(source,",") objRegExp.Pattern="(^,|,$)" source= objRegExp.Replace(source,"") End If If Instr(para,"[textarea]")>0 Then 'Set objRegExp=New RegExp 'objRegExp.IgnoreCase =True 'objRegExp.Global=True 'objRegExp.Pattern="(&)([#a-z0-9]{2,10})(;)" 'source= objRegExp.Replace(source,"&$2$3") source=Replace(source,"&","&") source=Replace(source,"%","%") source=Replace(source,"<","<") source=Replace(source,">",">") End If If Instr(para,"[japan-html]")>0 Then source=Replace(source,"ガ","ガ") source=Replace(source,"ギ","ギ") source=Replace(source,"ア","ア") source=Replace(source,"ゲ","ゲ") source=Replace(source,"ゴ","ゴ") source=Replace(source,"ザ","ザ") source=Replace(source,"ジ","ジ") source=Replace(source,"ズ","ズ") source=Replace(source,"ゼ","ゼ") source=Replace(source,"ゾ","ゾ") source=Replace(source,"ダ","ダ") source=Replace(source,"ヂ","ヂ") source=Replace(source,"ヅ","ヅ") source=Replace(source,"デ","デ") source=Replace(source,"ド","ド") source=Replace(source,"バ","バ") source=Replace(source,"パ","パ") source=Replace(source,"ビ","ビ") source=Replace(source,"ピ","ピ") source=Replace(source,"ブ","ブ") source=Replace(source,"ブ","ブ") source=Replace(source,"プ","プ") source=Replace(source,"ベ","ベ") source=Replace(source,"ペ","ペ") source=Replace(source,"ボ","ボ") source=Replace(source,"ポ","ポ") source=Replace(source,"ヴ","ヴ") End If If Instr(para,"[html-japan]")>0 Then source=Replace(source,"ガ","ガ") source=Replace(source,"ギ","ギ") source=Replace(source,"ア","ア") source=Replace(source,"ゲ","ゲ") source=Replace(source,"ゴ","ゴ") source=Replace(source,"ザ","ザ") source=Replace(source,"ジ","ジ") source=Replace(source,"ズ","ズ") source=Replace(source,"ゼ","ゼ") source=Replace(source,"ゾ","ゾ") source=Replace(source,"ダ","ダ") source=Replace(source,"ヂ","ヂ") source=Replace(source,"ヅ","ヅ") source=Replace(source,"デ","デ") source=Replace(source,"ド","ド") source=Replace(source,"バ","バ") source=Replace(source,"パ","パ") source=Replace(source,"ビ","ビ") source=Replace(source,"ピ","ピ") source=Replace(source,"ブ","ブ") source=Replace(source,"ブ","ブ") source=Replace(source,"プ","プ") source=Replace(source,"ベ","ベ") source=Replace(source,"ペ","ペ") source=Replace(source,"ボ","ボ") source=Replace(source,"ポ","ポ") source=Replace(source,"ヴ","ヴ") End If If Instr(para,"[html-format]")>0 Then source=Replace(source,"&","&") source=Replace(source,"<","<") source=Replace(source,">",">") source=Replace(source,"""",""") End If If Instr(para,"[anti-html-format]")>0 Then source=Replace(source,""","""") source=Replace(source,"<","<") source=Replace(source,">",">") source=Replace(source,"&","&") End If If Instr(para,"[wapnohtml]")>0 Then source=Replace(source,"<br/>",vbCrLf) source=Replace(source,"<br>",vbCrLf) Set objRegExp=New RegExp objRegExp.IgnoreCase =True objRegExp.Global=True objRegExp.Pattern="(<[^>]*)|([^<]*>)" source= objRegExp.Replace(source,"") objRegExp.Pattern="(\r\n|\n)" source= objRegExp.Replace(source,"<br/>") End If If Instr(para,"[nbsp-br]")>0 Then Set objRegExp=New RegExp objRegExp.IgnoreCase =True objRegExp.Global=True objRegExp.Pattern="<$|<b$|<br$|<br/$" source= objRegExp.Replace(source,"") objRegExp.Pattern="^br/>|^r/>|^/>|^>" source= objRegExp.Replace(source,"") objRegExp.Pattern="<br/>" source= objRegExp.Replace(source,"<br/>") objRegExp.Pattern="&nbsp;" source= objRegExp.Replace(source," ") End If If Instr(para,"[closehtml]")>0 Then source=closeHTML(source) End If If Instr(para,"[anti-zc_blog_host]")>0 Then source=Replace(source,GetCurrentHost(),"<#ZC_BLOG_HOST#>") End If TransferHTML=source End Function '*********************************************************
这是PHP版本的:
/** * 通过正则表达式格式化字符串 * @param string $source 字符串 * @param string $para 正则表达式,可用[html-format]|[nohtml]|[noscript]|[enter]|[noenter]|[filename]|[normalname]或自定义表达式 * @return string */ function TransferHTML($source, $para) { if (strpos($para, '[html-format]') !== false) { $source = htmlspecialchars($source); } if (strpos($para, '[nohtml]') !== false) { $source = preg_replace("/<([^<>]*)>/si", "", $source); $source = str_replace("<", "˂", $source); $source = str_replace(">", "˃", $source); } if (strpos($para, '[noscript]') !== false) { $source = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si", "", $source); $source = preg_replace("/<(\/?script.*?)>/si", "", $source); $source = preg_replace("/javascript/si", "", $source); $source = preg_replace("/vbscript/si", "", $source); $source = preg_replace("/on([a-z]+)\s*=/si", "on\\=", $source); } if (strpos($para, '[enter]') !== false) { $source = str_replace("\r\n", "<br/>", $source); $source = str_replace("\n", "<br/>", $source); $source = str_replace("\r", "<br/>", $source); $source = preg_replace("/(<br\/>)+/", "<br/>", $source); } if (strpos($para, '[noenter]') !== false) { $source = str_replace("\r\n", "", $source); $source = str_replace("\n", "", $source); $source = str_replace("\r", "", $source); } if (strpos($para, '[filename]') !== false) { $source = str_replace(array("/", "#", "$", "\\", ":", "?", "*", "\"", "<", ">", "|", " "), array(""), $source); } if (strpos($para, '[normalname]') !== false) { $source = str_replace(array("#", "$", "(", ")", "*", "+", "[", "]", "{", "}", "?", "\\", "^", "|", ":", "'", "\"", ";", "@", "~", "=", "%", "&"), array(""), $source); } return $source; }
突然想说ASP版本需要考虑的东西太尼玛多了...
转载请注明:鸟儿博客 » ZBLOG内置过滤函数