SoFunction
Updated on 2025-03-09

Check email integrity in php

<?php
if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]$",$email)) {
echo "Your E-Mail passes preliminary inspection";
}
?> 
In this sentence, first of all, an eregi function is applied, which is quite easy to understand. Just look for this book and you can give you an explanation:
Syntax: int ereg(string pattern, string string, array [regs]);
Return value: integer/array
This function uses the pattern rule to parse the string string.
The value returned by the comparison result is placed in the array parameter regs. The content of regs[0] is the original string string, regs[1] is the first string that conforms to the rules, regs[2] is the second string that conforms to the rules, and so on. If the parameter regs is omitted, it is just a simple comparison, and the return value is true if it is found.

What is not easy to understand is the previous regular expression: ^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]$
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;
"." That is ".", where "" is an escape character; "" means that the previous string can appear 2-3 times in a row. "()" means that the content contained must appear in the target object at the same time. "[_.0-9a-z-]" means any character contained in "_", ".", "-", letters from a to z range, and numbers from 0 to 9 range;
In this way, the regular expression can be translated like this:
"The following character must begin (^)", "The character must be included in "_", ".", "-", letters from a to z range, numbers from 0 to 9 range ([_.0-9a-z-])", "The previous character appears at least once (+)", @, "The string begins with a letter from a to z range, characters from 0 to 9 range, followed by at least one character contained in "-", any letter from a to z range, any number from 0 to 9 range, and ends with . (([0-9a-z][0-9a-z-]+.)), "The previous character appears at least once (+)", "The letters from a to z range appear 2-3 times and ends with it ([a-z]$)"
It's very complicated, right, right, because of this, people use regular expressions.