Example of php regular expression verification email address and email extraction

Source: Internet
Author: User
Tags php regular expression
Example of php regular expression verification email address and email extraction

  1. If (ereg ('/^ [a-z] ([a-z0-9] * [-_ \.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [\.] [a-z] {2, 3} ([\.] [a-z] {2 })? $/I; ", $ email )){
  2. Echo "Your email address is correct !";}
  3. Else {
  4. Echo "Please try again !";
  5. }
  6. ?>

The following describes how to use php regular expressions to match domain names.

We know that the international domain name format is as follows: the domain name is composed of a specific character set, English letters, numbers, and "-" (that is, a hyphen or minus sign) of the National text, however, the start and end cannot contain "-", and "-" cannot appear consecutively. The domain name is case-insensitive. The domain name can contain up to 60 bytes (including the suffix. com,. net, and. org ).

/^ [A-z] ([a-z0-9] * [-_]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [\.] [a-z] {2, 3} ([\.] [a-z] {2 })? $/I;/content/I forms a case-insensitive regular expression; ^ Match start $ match end [a-z] e-mail prefix must start with an English letter ([a-z0-9] * [-_]? [A-z0-9] +) * and _ a_2, aaa11, _ 1_a_2 match, and a1 _, aaff_33a _, a _ aa does not match, if it is a null character, but also match, * indicates 0 or more. * Represents 0 or more characters before. [a-z0-9] * matches 0 or more English letters or numbers [-_]? Match 0 or 1 '-' because '-' cannot appear consecutively [a-z0-9] + match 1 or more letters or numbers, because '-' cannot end @ must have a @ ([a-z0-9] * [-_]? [A-z0-9] +) + See above ([a-z0-9] * [-_]? [A-z0-9] +) * interpreted, but cannot be empty, + represents one or more. [\.] Set special characters (.) use it as a common character [a-z] {2, 3} to match 2 to 3 English letters, generally com or net. ([\.] [a-z] {2 })? Match 0 or 1 [\.] [a-z] {2} (for example. cn, etc.) I do not know whether the last part of .com.cn is generally two. if not, change {2} to {start word count, end word count}

2. extract the email from the string:

  1. Function getEmail ($ str ){
  2. $ Pattern = "/([a-z0-9] * [-_ \.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [\.] [a-z] {2, 3} ([\.] [a-z] {2 })? /I ";
  3. Preg_match_all ($ pattern, $ str, $ emailArr );
  4. Return $ emailArr [0];
  5. }
  6. $ Emailstr = "9999@qq.com.cn I'm not a vi place open iid mailing list: fuyongjie@163.com and hh@qq.com; ..;, fuyongjie.100@yahoo.com, fu-1999@sina.com ";
  7. $ EmailArr = getEmail ($ emailstr );
  8. Echo"
    ";
  9. print_r($emailArr);
  10. echo "
  11. ";
  12. ?>

Output result:

  1. Array
  2. (
  3. 9999@qq.com.cn
  4. Fuyongjie@163.com
  5. Hh@qq.com
  6. Fuyongjie.100@yahoo.com
  7. [4] => fu-1999@sina.com
  8. )

3. comparison: the regular expressions in 2nd do not contain the ^ and $ Values of 1st;

I would like to introduce this to you and hope it will be helpful for you to learn php regular expressions. Programmer's house. I wish you all a better learning experience.

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.