小刀

----像写情书一样写程序,像看小说一样看代码
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

PetShop文本输入过滤函数

Posted on 2007-12-04 15:08  Alex wu  阅读(190)  评论(0编辑  收藏  举报
/// <summary>
        
/// Method to make sure that user's inputs are not malicious
        
/// </summary>
        
/// <param name="text">User's Input</param>
        
/// <param name="maxLength">Maximum length of input</param>
        
/// <returns>The cleaned up version of the input</returns>

        public static string InputText(string text, int maxLength)
        
{
            text 
= text.Trim();
            
if (string.IsNullOrEmpty(text))
                
return string.Empty;
            
if (text.Length > maxLength)
                text 
= text.Substring(0, maxLength);
            text 
= Regex.Replace(text, "[\\s]{2,}"" ");    //two or more spaces
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)""\n");    //<br>
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+"" ");    //&nbsp;
            text = Regex.Replace(text, "<(.|\\n)*?>"string.Empty);    //any other tags
            text = text.Replace("'""''");
            
return text;
        }