Filter_var () Verify the format of the mailbox, IP, url php

Source: Internet
Author: User

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;

  1. /**
  2. * Verify if it is a mailbox
  3. * @param string $email mailbox
  4. * @return If Boolean is a mailbox
  5.  */
  6. function is_email($email) {
  7. if (Filter_var ($email, Filter_validate_email)) {
  8. return true;
  9. }else{
  10. return false;
  11. }
  12. }
  13. Var_dump (Is_email (' [email protected] '));
  14. //print ture
  15. Var_dump (Is_email (' baijunyao.com '));
  16. //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;

  1. /**
  2. * Verify if it is a URL
  3. * @param string $url URL
  4. * @return If Boolean is a URL
  5.  */
  6. function is_url($url) {
  7. if (Filter_var ($url, Filter_validate_url)) {
  8. return true;
  9. }else{
  10. return false;
  11. }
  12. }
  13. Var_dump (Is_url (' http://baijunyao.com ')); Print true
  14. Var_dump (Is_url (' baijunyao.com ')); Print false
  15. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.