PHP validation functions (including Email,url, date, etc.)

Source: Internet
Author: User

<?PHP/** * [email protected] Zouhao * Some verification methods*/  /** * is the mobile number * * @param string $phone phone number * @return Boolean*/  functionIs_phone ($phone) {      if(strlen($phone)! = 11 | | !Preg_match('/^1[3|4|5|8][0-9]\d{4,8}$/',$phone )) {          return false; } Else {          return true; }  }  /** * Verify that strings are numbers, letters, Chinese, and underscores * @param string $username * @return bool*/  functionIs_check_string ($str){      if(Preg_match('/^[\x{4e00}-\x{9fa5}\w_]+$/u ',$str)){          return true; }Else{          return false; }  }  /** * is a valid email * @param sting $email * @return Boolean*/  functionIs_email ($email){      if(Filter_var ($email,filter_validate_email)) {          return true; } Else {          return false; }  }/** * e-mail Verification can also use this method * This method for reference Ecshop*/  functionIs_email ($user _email){    $chars= "/^" ([a-z0-9+_]|\\-|\\.) [Email protected] (([a-z0-9_]|\\-) +\\.) +[a-z]{2,6}\$/i "; if(Strpos($user _email, ' @ ')!==false&&Strpos($user _email, ‘.‘) !==false)    {        if(Preg_match($chars,$user _email))        {            return true; }        Else        {            return false; }    }    Else    {        return false; }}  /** * is a valid URL * @param string $url * @return Boolean*/  functionIs_url ($url){      if(Filter_var ($url,Filter_validate_url)) {          return true; } Else {          return false; }  }  /** * is a valid IP address * @param string $ip * @return Boolean*/  functionIS_IP ($ip){      if(Ip2long($ip)) {          return true; } Else {          return false; }  }  /** * is an integer * @param int $number * @return Boolean*/  functionIs_number ($number){      if(Preg_match('/^[-\+]?\d+$/',$number)){          return true; }Else{          return false; }  }  /** * is a positive integer * @param int $number * @return Boolean*/  functionIs_positive_number ($number){      if(Ctype_digit($number)){          return true; }Else{          return false; }  }  /** * is a decimal * @param float $number * @return Boolean*/  functionIs_decimal ($number){      if(Preg_match('/^[-\+]?\d+ (\.\d+)? $/',$number)){          return true; }Else{          return false; }  }  /** * is a positive decimal number * @param float $number * @return Boolean*/  functionIs_positive_decimal ($number){      if(Preg_match('/^\d+ (\.\d+)? $/',$number)){          return true; }Else{          return false; }  }  /** * is English * @param string $str * @return Boolean*/  functionIs_english ($str){      if(Ctype_alpha($str))          return true; Else          return false; }  /** * is Chinese * @param string $str * @return Boolean*/  functionIs_chinese ($str){      if(Preg_match('/^[\x{4e00}-\x{9fa5}]+$/u ',$str))          return true; Else           return false; }  /** * To determine if it is a picture * @param string $file picture file path * @return Boolean*/  functionIs_image ($file){      if(file_exists($file) &&getimagesize($file===false)){          return false; }Else{          return true; }  }  /** * is a valid ID (supports 15-bit and 18-bit) * @param string $card * @return Boolean*/  functionIs_card ($card){      if(Preg_match('/^[1-9]\d{7} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3}$/',$card)||Preg_match('/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{4}$/',$card))          return true; Else           return false; }  /** * Verify that the date format is correct * @param string $date * @param string $format * @return Boolean*/  functionIs_date ($date,$format= ' y-m-d '){      $t=date_parse_from_format ($format,$date); if(Empty($t[' Errors '])){          return true; }Else{          return false; }  }  ?>

Some validation functions have just been collected.

PHP validation functions (including Email,url, dates, and so on)

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.