10 practical PHP Regular Expressions and php Regular Expressions

Source: Internet
Author: User

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.

Copy codeThe 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.

Copy codeThe 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.

Copy codeThe 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.

Copy codeThe 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.

Copy codeThe 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.

Copy codeThe 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.

Copy codeThe 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

Copy codeThe 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

Copy codeThe 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

Copy codeThe 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

Copy codeThe 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", '<span style = "background: #5fc9f6" >\1 </span> ', $ 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] (? <Num> \ 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]);

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.