//不需要正则表达式来判断,因为在php5.2.0之后,有专门的函数来做这个判断了。
//判断是否是合法IP
if(filter_var($ip, FILTER_VALIDATE_IP)) {
// it's valid
}
else {
// it's not valid
}
//判断是否是合法的IPv4 IP地址
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
// it's valid
}
else {
// it's not valid
}
//判断是否是合法的公共IPv4地址,192.168.1.1这类的私有IP地址将会排除在外
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {
// it's valid
}
else {
// it's not valid
}
//判断是否是合法的IPv6地址
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) {
// it's valid
}
else {
// it's not valid
}
//判断是否是public IPv4 IP或者是合法的Public IPv6 IP地址
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
// it's valid
}
else {
// it's not valid
}