PHP is verifying email addresses and extracting email examples from the content.

Source: Internet
Author: User
Tags learn php
    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. ?>
Copy Code

The following describes the method of matching the domain name with PHP.

We know that the international Domain name format is as follows: The domain name is a specific character set of national characters, English letters, numbers and "-" (that is, hyphens or minus) any combination, but the beginning and the end can not contain "-", "-" can not appear consecutively. The letters in the domain name are not case-sensitive. The domain name can be up to 60 bytes long (including suffix. com,. NET,. org, and so on).

/^[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] the e-mail prefix must be an English letter beginning ([a-z0-9]*[-_]?[ a-z0-9]+) * and _a_2, AAA11, _1_a_2 matches, and a1_, Aaff_33a_, A__aa do not match, if it is a null character, is also matched, * represents 0 or more. * Represents 0 or more of the preceding characters. [a-z0-9]* matches 0 or more English letters or numbers [-_]? Match 0 or 1 "-" because "-" cannot appear consecutively [a-z0-9]+ matches 1 or more English letters or numbers, because "-" cannot be the end @ must have a @ ([a-z0-9]*[-_]?[ a-z0-9]+) + See above ([a-z0-9]*[-_]?[ a-z0-9]+) * explanation, but cannot be empty, + denotes one or more. [\.] The special character (.) As a normal character [a-z]{2,3} matches 2 to 3 English letters, usually COM or net, and so on. ([\.] [A-z] {2})? Match 0 or 1 [\.] [A-z] {2} (e.g.. CN, etc.) I don't know the general. Com.cn the last part is not all two-bit, if not please modify {2} to {start Word, end Word}

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 am not a M VI place on the 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. ?>
Copy Code

Output Result:

    1. Array
    2. (
    3. [0] = 9999@qq.com.cn
    4. [1] = fuyongjie@163.com
    5. [2] = hh@qq.com
    6. [3] = fuyongjie.100@yahoo.com
    7. [4] = fu-1999@sina.com
    8. )
Copy Code

3, Comparison: 2nd in the regular there is no 1th ^ and $;

Just introduce these, I hope you learn PHP is a certain help. The programmer's home, wish everybody to study the progress.

  • 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.