日期:2014-05-17  浏览次数:20863 次

怎么屏蔽从数据库里读出来html代码
请教各位:
      我从数据库里读出来的html代码为什么在网页上原样显示?比如网页上显示成:
   
      <p> 文字 </p>          
   


------解决方案--------------------
保存时HTML.Encode
读取时HTML.Decode
------解决方案--------------------

function HtmlToStr(xhtml)
dim tmp,i
for i=1 to len(xhtml)
if mid(xhtml,i,1)= "& " then
tmp=tmp+ "&#38; "
elseif mid(xhtml,i,1)= "# " then tmp=tmp+ "&#35; "
elseif mid(xhtml,i,1)= "; " then tmp=tmp+ "&#59; "
elseif mid(xhtml,i,1)= " " then tmp=tmp+ "&nbsp; "
elseif mid(xhtml,i,1)= " ' " then tmp=tmp+ "&#39; "
elseif mid(xhtml,i,1)= "\ " then tmp=tmp+ "&#92; "
elseif mid(xhtml,i,1)= " < " then tmp=tmp+ "&lt; "
elseif mid(xhtml,i,1)= "> " then
tmp=tmp+ "&gt; "
else
tmp=tmp+mid(xhtml,i,1)
end if
next
HtmlToStr=replace(tmp,vbcrlf, " <br> ")
end function

Function StrToHTML(xstr)
xstr=replace(xstr, "&#38; ", "& ")
xstr=replace(xstr, "&#35; ", "# ")
xstr=replace(xstr, "&#59; ", "; ")
xstr=replace(xstr, "&nbsp; ", " ")
xstr=replace(xstr, "&#39; ", " ' ")
xstr=replace(xstr, "&#92; ", "\ ")
xstr=replace(xstr, " <br> ",vbcrlf)
xstr=replace(xstr, "&lt; ", " < ")
xstr=replace(xstr, "&gt; ", "> ")
StrToHTML=xstr
End Function
------解决方案--------------------


'清除HTML标记
Function stripHTML(htmlStr)
Dim regEx
SET regEx = New Regexp
regEx.IgnoreCase = True
regEx.Global = True
'regEx.Pattern = " <.*?> "
regEx.Pattern = " <(.[^> ]*)> "
htmlStr = regEx.Replace(htmlStr, " ")
htmlStr = Replace(htmlStr, " < ", "&lt; ")
htmlStr = Replace(htmlStr, "> ", "&gt; ")
htmlStr = Replace(htmlStr,chr(10), " ")
htmlStr = Replace(htmlStr,chr(13), " ")
stripHTML = Trim(htmlStr)
SET regEx = Nothing
End Function