您的位置:首页 > 其它

判断IP4类型的IP或IP段是否合法

2014-04-17 11:11 316 查看
/**
* IP是否合法
* @param string $ip
* @return boolean
*/
function inspect_ip($ip,$divide='/') {
    $divide = $divide ? $divide : '/';
    if (is_array($ip)) {
        return inspect_ip(implode($divide,$ip));
    }
    else if (strpos($ip,$divide)) {
        list($start,$end) = explode($divide, $ip);

        $pos = strrpos($start,'.');
        $start_d = substr($start,$pos+1);
        $c = substr($start,0,$pos+1);

        $pos = strrpos($end,'.');
        $end_d = substr($end,$pos+1);
        $c2 = substr($end,0,$pos+1);

        return inspect_ip($start) && inspect_ip($end) && $c == $c2 && $start != $end && $start_d < $end_d;
    }
    else if (filter_var($ip,FILTER_VALIDATE_IP)) {
        return true;
    }
    return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: