Home  >  Article  >  Backend Development  >  PHP validation daily format

PHP validation daily format

高洛峰
高洛峰Original
2016-10-21 10:35:181033browse

PHP验证日常格式_邮箱验证_手机验证_身份正验证_网址验证_时间验证

<?php
/*
*    Author : lemonice
*
*    Email:chengciming@126.com
*    
*    时间:2011-11
*
*    说明:验证日常格式(Email等)
*
*/
 
/**
 * 验证输入的邮件地址是否合法
 *
 * @access  public
 * @param   string      $user_email      需要验证的邮件地址
 *
 * @return bool
 */
function is_email($user_email){
    $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
    if (strpos($user_email, &#39;@&#39;) !== false && strpos($user_email, &#39;.&#39;) !== false){
        if (preg_match($chars, $user_email)){
            return true;
        }
        else{
            return false;
        }
    }
    else{
        return false;
    }
}
 
/**
 * 验证输入的手机号码
 *
 * @access  public
 * @param   string      $user_mobile      需要验证的手机号码
 *
 * @return bool
 */
function is_mobile($user_mobile){
    $chars = "/^((\(\d{2,3}\))|(\d{3}\-))?1(3|5|8|9)\d{9}$/";
     
    if (preg_match($chars, $user_mobile)){
        return true;
    }else{
        return false;
    }
}
 
/**
 * 验证输入的电话号码
 *
 * @access  public
 * @param   string      $user_phone      需要验证的电话号码
 *
 * @return bool
 */
function is_phone($user_phone){
        $chars = "/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/";
    if (preg_match($chars, $user_phone)){
        return true;
    }else{
        return false;
    }
}
 
/**
 * 验证输入的网址
 *
 * @access  public
 * @param   string      $user_url      需要验证的网址
 *
 * @return bool
 */
function is_url($user_url){
    $chars = "/((^http)|(^https)|(^ftp)):\/\/(\S)+\.(\w)+/";
    if (preg_match($chars, $user_url)){
        return true;
    }else{
        return false;
    }
}
 
/**
 * 验证输入的字符串是否带有特殊符号
 *
 * @access  public
 * @param   string      $user_safe      需要验证的字符串
 *
 * @return bool
 */
function is_safe($user_safe){
    $chars = "/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\&#39;\"]*)|.{0,5})$|\s/";
if (!preg_match($chars, $user_safe)){
        return true;   //找不到特殊字符则返回true
    }else{
        return false;
    }
}
 
/**
 * 检查是否为一个合法的时间格式
 *
 * @access  public
 * @param   string  $time   格式:2011-11-16 15:54:13
 * @return  void
 */
function is_time($time){
    $pattern = &#39;/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/&#39;;
    return preg_match($pattern, $time);
}
?>


Statement:
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