php正则表达式验证IP格式、MAC格式、掩码格式、时间格式、16进制数据、文件路径

//ip格式验证
 

function IsValid_Ip($ip){
    if (strlen($ip) == 0)
        return 0;
    return preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip)
}

//mac格式验证,两种形式的mac地址书写
 

function IsValid_Mac($mac){
    if (strlen($mac) == 0)
        return 0;
  if(preg_match("/^[A-Fa-f0-9]{1,2}\-[A-Fa-f0-9]{1,2}\-[A-Fa-f0-9]{1,2}\-[A-Fa-f0-9]{1,2}\-[A-Fa-f0-9]{1,2}\-[A-Fa-f0-9]{1,2}$/", $mac))
    {
        return 1;
    } else if(preg_match("/^[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}\:[A-Fa-f0-9]{1,2}$/", $mac)) {
        return 2;
    }
    else{
        return 0;
    }
}

//掩码

function Mask_Judge($mask) {
    if (strlen($mask) == 0 || !IsValid_Ip($mask))
        return 0;
    $bin_mask  = (string)decbin(ip2long($mask));
    $pos_0 = strpos($bin_mask,"0");
    $pos_1 = strrpos($bin_mask,"1");
    if($pos_0==0||$pos_0==$pos_1+1||$pos_1==31) {
        return 1;
    } else {
        return 0;
    }
}

//时间格式

function Time_Judge($date) {
    if (gettype($date) == 'string')  //判断字段类型,如果是字符串,时间格式00:00
    {
        $d = explode(":",$date);
        if (count($d) != 2)
            return 0;
        if (!is_numeric($d[0]) || !is_numeric($d[1]))
            return 0;
        if (((int)$d[0]) < 0 || ((int)$d[0]) > 23)
            return 0;
        if (((int)$d[1]) < 0 || ((int)$d[1]) > 59)
            return 0;
        return 1;
    }

    if (strlen($date) == 0)   //如果是小数形式
        return 0;
    $h = (int)($date*24);
    $m = (int)(($date*24*60)%60);
    if(strlen($h)==1) $h = '0'.$h;
    if(strlen($m)==1) $m = '0'.$m;
    $t = $h.':'.$m;
    if(preg_match('/^((1|0?)[0-9]|2[0-3]):([0-5][0-9])$/', $t)) {
        return 1;
    } else {
        return 0;
    }
}

//验证16进制数据

$pattern = "/(0x)[0-9a-fA-F]{1,4}|[0-9a-fA-F]{1,4}(H)/i";
   return preg_match($pattern, $func);

//文件路径/受限进程路径/windows文件名命名规则
 

function Path_Judge($data) {
    $data = (string)$data;
    if (strlen($data) == 0)
        return 0;
    $pattern = '#^[a-zA-Z]\:\\\\[^/\:\*\?\"\<\>|]*$#';
    if(preg_match($pattern, $data)) {
        return 1;
    } else {
        return 0;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值