Php Chinese characters, letters, numbers, underscores, regular expressions

Source: Internet
Author: User
Tags chr comments regular expression

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.