Regular expression example: check email integrity in php

高洛峰
Release: 2023-03-02 20:18:01
Original
952 people have browsed it

if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]$", $email)) {
echo "Your email passed the preliminary check";
}
?>
In this sentence, first of all, an eregi function is applied, which is fairly easy to understand. Just look for this book and it will give you an explanation:
Syntax: int ereg(string pattern, string string, array [regs]);
Return value: integer/array
This function uses pattern rules to parse and compare strings 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, 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-]+.) +[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 the previous string Must appear at the end;
"." is ".", where "" is an escape character; "" 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 be at the beginning (^)", "The characters must be contained in "_", ".", "-", from a to z Letters, numbers in the range from 0 to 9 ([_.0-9a-z-])", "The preceding character appears at least once (+)", @, "The string consists of a string from a to Starting with a letter in the range z, a number in the range 0 to 9, followed by at least one character contained in "-", any letter in the range a to z, any number in the range 0 to 9 characters, ending with . (([0-9a-z][0-9a-z-]+.))", "The previous character appears at least once (+)", "from a to z The letter appears 2-3 times and ends with it ([a-z]$)"
It's complicated, right. That's why people use regular expressions.


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template