当读取以后是否还需要转换什么的

解决方案 »

  1.   

    直接存入,读取时,如果没有危险标记,比如<script>window.location='http://www.xxx.com'</script>,可直接输出,如果有这类危险标记(或是会影响页面布局的不完整html标记),可考虑用正则表达式替换去掉,最好的做法,是在存入前,就把不需要的标签先去掉,仅把安全的html标签按原样存入即可
      

  2.   

    楼上的说可考虑用正则表达式替换去掉,你所说的正规表达式是什么?用什么语言写啦?是javascrip还是C#等其他什么啦?能不能说的清楚一点
      

  3.   

    一般从表单里提交到数据库的内容肯定要先过滤的。比如<script><iframe><framset><a href="">这些标签把他们替换了就可以了。像<html>标签也要过滤,一般留言什么的显示出来后你肯定不想执行成网页的布局吧,肯定是显示成字符串形式。
    代码如下: //过滤所有script标记
            public string wipeScript(string html)
            {
                System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                html = regex1.Replace(html, ""); //过滤<script></script>标记
                html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
                html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
                html = regex4.Replace(html, ""); //过滤iframe
                html = regex5.Replace(html, ""); //过滤frameset
                return html;
            }