Php determines that the IP address is a valid IP address.
When most people see this log, the first impression must be about how to use regular expressions to determine.
No. After php5.2.0, a special function is provided for this judgment.
Determine whether the IP address is valid
if(filter_var($ip, FILTER_VALIDATE_IP)) {// it's valid}else {// it's not valid}
Determine whether an IPv4 IP address is valid
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {// it's valid}else {// it's not valid}
Check whether it is a legal public IPv4 address. Private IP addresses such as 192.168.1.1 will be excluded.
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {// it's valid}else {// it's not valid}
Determine whether an IPv6 address is valid
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) {// it's valid}else {// it's not valid}
Determine whether it is a public IPv4 IP address or a legal Public IPv6 IP Address
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}
Source: http://www.electrictoolbox.com/php-validate-ip-address-filter-var/
Generally, we can use regular expressions for implementation. For details, refer to this article.