This article mainly introduces PHP using the Preg_match () function to verify the IP address of the method, involving PHP for the number and string of regular matching operations related skills, the need for friends can refer to the following
This example describes how PHP uses the Preg_match () function to validate IP addresses. Share to everyone for your reference, as follows:
Code one, regular implementation
Preg_match ('/^ (?: 25[0-5]|2[0-4]\d|1\d\d|[ 1-9]\d|\d) (?: [.] (?: 25[0-5]|2[0-4]\d|1\d\d| [1-9]\d|\d]) {3}$/', $ipAddress);
Code two,
<?php/** @return boolen* @param string $ip to match the IP address * @param string $pat match the regular rule * @param the Boolean value returned after the Boolen match succeeds *preg_match () * 0 for unsuccessful, 1 for Success */function Fun ($IP) { //0.0.0.0---255.255.255.255 $pat = "/^ (((1?\d{1,2}) | ( 2[0-4]\D) | (25[0-5])) \.) {3} ((1?\d{1,2}) | (2[0-4]\d) | (25[0-5])) $/"; if (Preg_match ($pat, $ip)) { $num = Preg_match ($pat, $ip); return $num; } else{ $num = Preg_match ($pat, $ip); return $num; }} echo Fun ("255.255.255.255");
The efficiency of the regular is less than the original, so drop a link (filter function) to leave.
Filter options, such as the ability to filter private IP addresses.
Usage reference validating an IP address with PHP ' s Filter_var function
How PHP determines IP as a valid IP address
When most people see this log, the first impression must be to say how to judge through regular expressions.
Not also, after php5.2.0, there is a special function to do this judgment.
Determine if it is a legitimate IP
if (Filter_var ($ip, filter_validate_ip)) {//it ' s valid}else {//It's not valid}
Determine if the IPV4 IP address is valid
if (Filter_var ($ip, Filter_validate_ip, Filter_flag_ipv4)) {//it ' s valid}else {//It's not valid}
Determine if it is a valid public IPv4 address, 192.168.1.1 This type of private IP address 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 if the IPV6 address is valid
if (Filter_var ($ip, Filter_validate_ip, Filter_flag_no_res_range)) {//it ' s valid}else {//It's not valid}
Determines whether the public IPv4 IP or is a legitimate 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}
The above is the whole content of this article, I hope that everyone's study has helped.