I would like to share several common functions that will help you.
I. Because it is similar to posts, there is often a post in the News list. At this time, you have to filter html tags. There are a lot of web Baidu websites that filter html tags. The following is a function used to filter images in a summary:
Public string removeIme (string s)
{
String str = s;
Str. Replace ("IMG", "img ");
String reg = ")] * src \ s * = \ s }(? <SRC> [^ (\ s \ "'>)] *) [\"'] {0, 1} \ s {0, 1} [^>] *> ";
While (Regex. IsMatch (str, reg ))
{
Str = str. Replace (Regex. Match (str, reg). Value, "image ..");
}
R
Ii. SQL injection attacks are a security challenge for web websites. Anti-injection has become an important course. In addition to SQL statements and stored procedures, I will share a function for detecting input by foreground users.
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;
}
Iii. ubb is a popular and complete code. It is also one of the measures to prevent SQL injection and cross-site scripting. Share the function that converts ubb into html code.
Public static string decode (string argString)
{
String tString = argString;
If (tString! = "")
{
Regex tRegex;
Bool tState = true;
TString = tString. Replace ("&", "& amp ;");
TString = tString. Replace (">", "& gt ;");
TString = tString. Replace ("<", "& lt ;");
TString = tString. Replace ("\" "," & quot ;");
TString = Regex. Replace (tString, @ "\ [br \]", "<br/>", RegexOptions. IgnoreCase );
String [,] tRegexAry = {
{@ "\ [P \] ([^ \ [] *?) \ [\/P \] "," $1 <br/> "},
{@ "\ [B \] ([^ \ [] *?) \ [\/B \] "," <B> $1 </B> "},
{@ "\ [I \] ([^ \ [] *?) \ [\/I \] "," <I> $1 </I> "},
{@ "\ [U \] ([^ \ [] *?) \ [\/U \] "," <u >1 1 </u> "},
{@ "\ [Ol \] ([^ \ [] *?) \ [\/Ol \] "," <ol> $1 </ol> "},
{@ "\ [Ul \] ([^ \ [] *?) \ [\/Ul \] "," <ul> $1 </ul> "},
{@ "\ [Li \] ([^ \ [] *?) \ [\/Li \] "," <li >1 1 </li> "},
{@ "\ [Code \] ([^ \ [] *?) \ [\/Code \] "," <div class = \ "ubb_code \"> $1 </div> "},
{@ "\ [Quote \] ([^ \ [] *?) \ [\/Quote \] "," <div class = \ "ubb_quote \"> $1 </div> "},
{@ "\ [Color = ([^ \] *) \] ([^ \ [] *?) \ [\/Color \] "," <font style = \ "color: $1 \"> $2 </font> "},
{@ "\ [Hilitecolor = ([^ \] *) \] ([^ \ [] *?) \ [\/Hilitecolor \] "," <font style = \ "background-color: $1 \"> $2 </font> "},
{@ "\ [Align = ([^ \] *) \] ([^ \ [] *?) \ [\/Align \] "," <div style = \ "text-align: $1 \"> $2 </div> "},
{@ "\ [Url = ([^ \] *) \] ([^ \ [] *?) \ [\/Url \] "," <a href = \ "$1 \"> $2 </a> "},
{@ "\ [Img \] ([^ \ [] *?) \ [\/Img \] "," "}
};
While (tState)
{
TState = false;
For (int ti = 0; ti <tRegexAry. GetLength (0); ti ++)
{
TRegex = new Regex (tRegexAry [ti, 0], RegexOptions. IgnoreCase );
If (tRegex. Match (tString). Success)
{
TState = true;
TString = Regex. Replace (tString, tRegexAry [ti, 0], tRegexAry [ti, 1], RegexOptions. IgnoreCase );
}
}
}
}
Return tString;
}
From qingniu Ke