2015-12-30 12:33:53 | 作者:老鐵SEO | 0個評論 | 人瀏覽
<%
'格式化提交的特殊字符【凌陳亮(QQ:57404811)】
'參數:類型(1為SQL查詢字符串時使用,0和其它為輸出到原表單元素中時使用),字符串
function replace_yinhao(typ,str)
dim f_str : f_str=str
if len(f_str)>0 then
if typ=1 then 'SQL查詢語句時使用
f_str=replace(f_str,"'","''")
f_str=replace(f_str,"""","""""")
else '輸出到原表單元素中時使用
f_str=replace(f_str,"&","&")
f_str=replace(f_str,"#","#")
f_str=replace(f_str," "," ")
f_str=replace(f_str,"'","'")
f_str=replace(f_str,"""",""")
f_str=replace(f_str,"<","<")
f_str=replace(f_str,">",">")
end if
end if
replace_yinhao=f_str
end function
%>
調用案例:
1、SQL查詢語句時使用
<%
dim str
str="恭喜'凌陳亮博客'開通!"
'正確的寫法:
sql="select * from [tb] where [title] like '%" & replace_yinhao(1,str) & "%'"
'一般SQL查詢語句是這樣寫的:
'sql="select * from [tb] where [title] like '%" & str & "%'"
'其實SQL真實查詢時是這樣的:
'sql="select * from [tb] where [title] like '%恭喜'凌陳亮博客'開通!%'"
'于是就會出現語法錯誤,使用此函數轉化后就沒有錯誤了。
%>
2、輸出到原表單元素中時使用
<%
dim str
str="恭喜""凌陳亮博客""開通!"
%>
【正確的寫法】
使用此函數轉化后輸出:
<input type="text" name="title" id="title" value="<%=replace_yinhao(0,str)%>" />
真實輸出是這樣的:
<input type="text" name="title" id="title" value="恭喜"凌陳亮博客"開通!" />
【錯誤的寫法】
一般會這樣寫:
<input type="text" name="title" id="title" value="<%=str%>" />
其實真實輸出是這樣的:
<input type="text" name="title" id="title" value="恭喜"凌陳亮博客"開通!" />
于是就會出現格式錯亂,使用此函數轉化后就顯示正確了。