In php, there are user-defined functions that can help you determine the IP address, and we also use regular functions to determine the IP address. Below I will summarize some methods to verify the IP address. In php, there are user-defined functions that can help you determine the IP address, and we also use regular functions to determine the IP address. Below I will summarize some methods to verify the IP address.
Script ec (2); script
The filter function filters IP addresses by using the following methods:
| The Code is as follows: |
|
|
Echo filter_var ("127.0.0.1", "FILTER_VALIDATE_INT"); // return true or false |
Example.
Determine whether the IP address is valid
| The Code is as follows: |
|
|
If (filter_var ($ ip, FILTER_VALIDATE_IP )){
// 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.
| The Code is as follows: |
|
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
| The Code is as follows: |
|
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
| The Code is as follows: |
|
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
} |
Simple Regular Expressions
Preg_match ("/d {1, 3 }. d {1, 3 }. d {1, 3 }. d {1, 3}/"); // This method is not accurate. The maximum value is 255.255.255.255, but this is only the verification format. For example, 999.999.999.999 can also be verified.
Upgrade and Improvement
| The Code is as follows: |
|
|
Functionis_ip ($ gonten ){
$ Ip = explode (".", $ gonten );
For ($ I = 0; $ I
{
If ($ ip [$ I]> 255 ){
Return (0 );
}
}
Return ereg ("^ [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3} $ ", $ gonten );
} |
This meets our requirements.
Next we will introduce a method to determine IP addresses:
| The Code is as follows: |
|
|
Function matchip ($ q ){
Preg_match ('/(25 [0-5]) | (2 [0-4] d) | (1dd) | ([1-9] d) | d) (. (25 [0-5]) | (2 [0-4] d) | (1dd) | ([1-9] d) | d )) {3}/', $ q, $ matches );
Return $ matches [0];
}
$ Ipaddress = '1970. 103.2.2 ';
$ Iperror = '2017. 3.6.6 ';
$ Iptest = matchip ($ ipaddress );
// When the value of matchip is $ ipaddress, the output is 201.103.2.2.
// When the value of the function given to matchip is $ iperror, the output value is 62.3.6.6
If ($ iptest)
{
Echo $ iptest;
}
Else
{
Echo 'www .111cn.net prompt: the IP address you entered is incorrect ';
} |