Regular expression matching the first and last blank characters: ^ s * | s * $
Comments: It can be used to delete spaces (including spaces, tabs, page breaks, and so on) at the beginning and end of a line. It is a very useful expression.
$ Action = trim ($ _ get ['action']);
If ($ action = "sub ")
{
$ Str = $ _ post ['dir'];
// If (! Preg_match ("/^ [". chr (0xa1 ). "-". chr (0xff ). "A-Za-z0-9 _] + $/", $ str) // gb2312 regular expression of Chinese characters, letters, numbers, underscores (_)
If (! Preg_match ("/^ [x {4e00}-x {9fa5} A-Za-z0-9 _] + $/u", $ str) // regular expression of UTF-8 Chinese characters, letters, numbers, underscores (_)
{
Echo "<font color = red> the [". $ str. "] you entered contains illegal characters </font> ";
}
Else
{
Echo "<font color = green> the [". $ str. "] you entered is completely legal. Pass! </Font> ";
}
}
?>
<Form method = "post" action = "">
Input characters (numbers, letters, Chinese characters, and underlines ):
<Input type = "text" name = "dir" value = "">
<Input type = "submit" value = "submit">
</Form>
Regular Expression Matching url: [a-za-z] +: // [^ s] *
Comments: versions circulating on the Internet have limited functions. The above can basically meet the requirements.
Match account validity (starting with a letter, may be 5-16 bytes, may be an alphanumeric underline): ^ [a-za-z] [A-Za-z0-9 _] {} $
Comment: form verification is very useful
Match Chinese phone number: d {3}-d {8} | d {4}-d {7}
Commentary: matching forms such as 0511-4405222 or 021-87888822
Match Tencent QQ number: [1-9] [0-9] {4 ,}
Comment: Tencent QQ number starts from 10000
Match China Zip code: [1-9] d {5 }(?! D)
Note: China post code is a six-digit number.
Matched ID card: d {15} | d {18}
Note: the ID card of China is 15 or 18 characters
Matched IP address: d +. d +
Comments: useful when extracting IP addresses