IP address is a network protocol address commonly used on the computer Internet. Common formats include IPv4 and IPv6. The IPv4 address is represented by four 8-bit binary numbers. Each binary number ranges from 0 to 255. Each Binary numbers are separated by ., for example, 192.168.1.1. In a program, sometimes it is necessary to convert an IP address into a corresponding integer, or to convert an integer into a corresponding IP address. This involves conversion between IP addresses and integers.
In PHP programs, we can use some built-in functions to convert between IP addresses and integers. PHP's IP address conversion function is ip2long(). This function accepts an IPv4 string parameter and returns the corresponding 32-bit unsigned integer. If the passed IPv4 address is not a valid IPv4 address, the function returns false. For example, to convert 192.168.1.1 to an integer you can use the following code:
$ip = '192.168.1.1'; $ip_long = ip2long($ip); echo $ip_long;
The output is: -1062731775. It should be noted that since PHP uses 32-bit machine words, the returned integer is signed. If you need an unsigned integer, you can use the following code:
$long = sprintf("%u", $ip_long); echo $long;
The output result is: 3232235775.
If you need to convert an integer into the corresponding IP address, you can use PHP's long2ip() function. This function accepts a 32-bit unsigned integer parameter and returns the corresponding IPv4 address string. For example, to convert -1062731775 to the corresponding IP address, you can use the following code:
$ip_long = -1062731775; $ip = long2ip($ip_long); echo $ip;
The output result is: 192.168.1.1.
It should be noted that the parameter of the long2ip() function must be a 32-bit unsigned integer. If a signed integer or a 64-bit integer is passed, the IP address returned by the function may be wrong. If you need to convert a signed integer into an unsigned integer, you can use PHP's sprintf() function, which can output a string in a specified format into a variable, for example:
$long = sprintf("%u", $ip_long); $ip = long2ip($long); echo $ip;
The above code can be used correctly Convert the signed integer to an unsigned integer, and then convert the unsigned integer into the corresponding IPv4 address string.
In short, the process of converting IP addresses and integers in PHP programs is very simple. You only need to use the built-in ip2long() and long2ip() functions. At present, IPv6 addresses have also been widely used. If you need to process IPv6 addresses, you can use the inet_pton() and inet_ntop() functions in PHP. These two functions can convert IPv6 addresses into corresponding binary strings and reverse conversion, but Processing IPv6 addresses is more complex than IPv4 addresses and requires the use of extension libraries or writing your own code.
The above is the detailed content of How to convert IP address and integer in php. For more information, please follow other related articles on the PHP Chinese website!