- 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 )){
- Echo "Your email address is correct !";}
- Else {
- Echo "Please try again !";
- }
- ?>
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:
- Function getEmail ($ str ){
- $ Pattern = "/([a-z0-9] * [-_ \.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [\.] [a-z] {2, 3} ([\.] [a-z] {2 })? /I ";
- Preg_match_all ($ pattern, $ str, $ emailArr );
- Return $ emailArr [0];
- }
- $ 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 ";
- $ EmailArr = getEmail ($ emailstr );
- Echo"
"; - print_r($emailArr);
- echo "
";
- ?>
-
Output result:
- Array
- (
- 9999@qq.com.cn
- Fuyongjie@163.com
- Hh@qq.com
- Fuyongjie.100@yahoo.com
- [4] => fu-1999@sina.com
- )
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. |