The best life is use of willing attitude, a happy-go-lucky life.
— Mr.Wang
发布时间:2019-02-20 15:12:56
发布作者:admin
4883
不需要正则表达式来判断,因为在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 }