10 Practical PHP regular expressions and php regular expressions

Source: Internet
Author: User
10 Practical PHP regular expressions and php regular expressions. 10 Practical PHP regular expressions, and php regular expressions. This article describes 10 Practical PHP regular expressions for your reference. Details: 10 Practical PHP regular expressions and php regular expressions

This article provides 10 Practical PHP regular expressions for your reference. The details are as follows:

Regular expressions are an important element in program development. they provide strings used to describe or match text, such as specific characters, words, or expressions. However, in some cases, it is complicated and time-consuming to use a regular expression to verify a string. This article introduces you to the writing of 10 common practical PHP regular expressions, hoping to help your work.

1. verify the email address

This is a regular expression used to verify the email. But it is not an efficient and perfect solution. It is not recommended here.

The code is as follows:

$ Email = "test@ansoncheung.tk ";
If (preg_match ('/^ [^ 0-9] [a-zA-Z0-9 _] + ([.] [a-zA-Z0-9 _] +) * [@] [a-zA-Z0-9 _] + ([.] [a-zA-Z0-9 _] +) * [.] [a-zA-Z] {2, 4} $/', $ email )){
Echo "Your email is OK .";
} Else {
Echo "Wrong email address format ";
}


Filer_var is recommended for more effective email address verification.

The code is as follows:

If (filter_var ('test + email @ ansoncheung ', FILTER_VALIDATE_EMAIL )){
Echo "Your email is OK .";
} Else {
Echo "Wrong email address format .";
}

2. verify the user name

This is an instance used to authenticate the user name, including letters, numbers (A-Z, a-z, 0-9), underscores, and a minimum of 5 characters, a maximum of 20 characters. You can also modify the minimum and maximum values as needed.

The code is as follows:

$ Username = "user_name12 ";
If (preg_match ('/^ [a-z \ d _] {5, 20} $/I', $ username )){
Echo "Your username is OK .";
} Else {
Echo "Wrong username format .";
}

3. verify the phone number

This is an example of US phone number verification.

The code is as follows:

$ Phone = "(021) 423-2323 ";
If (preg_match ('/\(? \ D {3 }\)? [-\ S.]? \ D {3} [-\ s.] \ d {4}/X', $ phone )){
Echo "Your phone number is OK .";
} Else {
Echo "Wrong phone number .";
}

4. verify the IP address

This is an instance used to verify the IPv4 address.

The code is as follows:

$ IP = "198.168.1.78 ";
If (preg_match ('/^ ([1-9]? [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5]). {3} ([1-9]? [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5]) $ /', $ IP )){
Echo "Your IP address is OK .";
} Else {
Echo "Wrong IP address .";
}

5. verify the ZIP code

This is an instance used to verify the zip code.

The code is as follows:

$ Zipcode = "12345-5434 ";
If (preg_match ("/^ ([0-9] {5}) (-[0-9] {4 })? $/I ", $ zipcode )){
Echo "Your Zip code is OK .";
} Else {
Echo "Wrong Zip code .";
}

6. verify SSN (social insurance number)

This is an instance that verifies SSN in the United States.

The code is as follows:

$ Ssn = "333-23-2329 ";
If (preg_match ('/^ [\ d] {3}-[\ d] {2}-[\ d] {4} $/', $ ssn )){
Echo "Your SSN is OK .";
} Else {
Echo "Wrong SSN .";
}

7. verify the credit card number

The code is as follows:

$ Cc = "378282246310005 ";
If (preg_match ('/^ (? : 4 [0-9] {12 }(? : [0-9] {3 })? | 5 [1-5] [0-9] {14 }| 6011 [0-9] {12} | 3 (? : 0 [0-5] | [68] [0-9]) [0-9] {11} | 3 [47] [0-9] {13 }) $/', $ cc )){
Echo "Your credit card number is OK .";
} Else {
Echo "Wrong credit card number .";
}

8. verify the domain name

The code is as follows:

$ Url = "http://ansoncheung.tk /";
If (preg_match ('/^ (http | https | ftp): \ // ([A-Z0-9] [A-Z0-9 _-] * (? : \. [A-Z0-9] [A-Z0-9 _-] *) + ):? (\ D + )? \/? /I ', $ url )){
Echo "Your url is OK .";
} Else {
Echo "Wrong url .";
}

9. extract domain names from a specific URL

The code is as follows:

$ Url = "http://ansoncheung.tk/articles ";
Preg_match ('@ ^ (? : Http ://)? ([^/] +) @ I ', $ url, $ matches );
$ Host = $ matches [1];
Echo $ host;

10. highlight keywords in the text

The code is as follows:

$ Text = "Sample sentence from AnsonCheung. tk, regular expression has become popular in web programming. now we learn regex. according to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor ";
$ Text = preg_replace ("/\ B (regex) \ B/I", '\ 1', $ text );
Echo $ text;

I hope this article will help you with PHP programming.


How does php extract 10 consecutive digits from a piece of content using a regular expression?

[^ \ D] (? \ D {10}) ([^ \ d] | $)

How does PHP use variables in a regular expression?

Int preg_match (string $ pattern, string $ subject [, array $ matches [, int $ flags])
String $ pattern is a string, so you can calculate the string and store it in a variable.
$ Star = 'a ';
$ Stop = 'C ';
$ Info = 'a1b2c3 ';
$ Pattern = '/'. $ star. '(. + ?) '. $ Stop .'/';
Preg_match ($ pattern, $ info, $ result );
Print_r ($ result [1]);

Examples in this article describe 10 Practical PHP regular expressions for your reference. The details are as follows: regular...

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.