An explanation of commonly used email verification statements in PHP
Author:Eve Cole
Update Time:2009-06-06 18:17:18
I believe that anyone who has studied PHP well should know the following statement for email verification, but not many can really understand it:
<?php
if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[az]{2,3}$",$ email)) {
echo "Your email passed preliminary inspection";
}
?>
In this sentence, the first thing is to apply an eregi function, which is fairly easy to understand. Just find a book and it will give you an explanation:
Syntax: int ereg(string pattern, string string, array [regs]);
Return value: integer/array This function parses and compares strings based on pattern rules.
The value returned by the comparison result is placed in the array parameter regs. The content of regs[0] is the original string, regs[1] is the first string that conforms to the rules, and regs[2] is the second string that conforms to the rules. string, and so on. If the parameter regs is omitted, it will simply be compared, and the return value will be true if found.
What is not easy to understand is the previous regular expression: ^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[ az]{2,3}$
In this regular expression, "+" means that the previous string appears one or more consecutively; "^" means that the next string must appear at the beginning, and "$" means that the previous string must appear at the end;
"." is also ".", where "" is an escape character; "{2,3}" means that the previous string can appear 2-3 times in a row. "()" means that the contained content must also appear in the target object. "[_.0-9a-z-]" means any character contained in "_", ".", "-", letters in the range from a to z, and numbers in the range from 0 to 9;
In this way, this regular expression can be translated like this:
"The following characters must begin with (^)", "The character must be contained in "_", ".", "-", letters in the range from a to z, numbers in the range from 0 to 9 ([ _.0-9a-z-])", "The preceding character appears at least once (+)", @, "The string consists of a letter contained in the range from a to z, from 0 to 9 begins with a character in a number, followed by at least one character contained in "-", any letter in the range from a to z, any number in the range from 0 to 9, and finally ends with . (([0-9a -z][0-9a-z-]+.))", "The previous character appears at least once (+)", "Letters in the range from a to z appear 2-3 times and end with it ([ az]{2,3}$)”
It's complicated, right? That's why people use regular expressions.