Verify that the mailbox format is correct or not; What's your first solution?
No matter what you think, the first thing I think about is the string lookup to see if there is an @ symbol;
But for the end of. com or. NET OR. cn and so on more and more domain name verification feel tricky;
Until one day, the parameters of the Filter_var () function were studied carefully.
I am deeply ashamed to read PHP documents before.
In fact Filter_var () the second parameter Filter_validate_email can verify the mailbox, a few finishing is as follows;
- /**
- * Verify if it is a mailbox
- * @param string $email mailbox
- * @return If Boolean is a mailbox
- */
- function is_email($email) {
- if (Filter_var ($email, Filter_validate_email)) {
- return true;
- }else{
- return false;
- }
- }
- Var_dump (Is_email (' [email protected] '));
- //print ture
- Var_dump (Is_email (' baijunyao.com '));
- //Print false
This function verifies that the rule for the mailbox is to have an @ sign, and then a full stop after the @ sign. ;
Hey? See Baijunyao.com; There is a function that can verify the URL domain name?
Yes; the second argument filter_validate_url is used to verify the URL;
- /**
- * Verify if it is a URL
- * @param string $url URL
- * @return If Boolean is a URL
- */
- function is_url($url) {
- if (Filter_var ($url, Filter_validate_url)) {
- return true;
- }else{
- return false;
- }
- }
- Var_dump (Is_url (' http://baijunyao.com ')); Print true
- Var_dump (Is_url (' baijunyao.com ')); Print false
- Var_dump (Is_url (' http://a ')); Print true
According to the printed results, it is necessary to judge whether the rules of the URL are to have http://followed by an alphanumeric number;
Of course, the second parameter is filter_validate_ip to verify whether it is an IP address;
The Filter_var is very powerful, so is it possible to pass a filter_validate_phone to verify the phone number?
nn;;; the answer is no; you think more;
This is used for cover-up;
Reprint 51059529
Filter_var () Verify the format of the mailbox, IP, url php