php Method to convert ipv6 to ipv4: 1. Create a PHP sample file; 2. Define a variable "$ipv6 = '2a01:4f8:190:4413::2';"; 3. Use hexdec and The substr function converts the ipv6 address to an ipv4 address.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
php How to convert ipv6 to ipv4?
php code to convert ipv6 address to ipv4 address
The code is as follows:
$ipv6 = '2a01:4f8:190:4413::2'; $ipv4 = hexdec(substr($ipv6, 0, 2)). "." . hexdec(substr($ipv6, 2, 2)). "." . hexdec(substr($ipv6, 5, 2)). "." . hexdec(substr($ipv6, 7, 2)); echo $ipv4;
hexdec () function converts hexadecimal to decimal.
Syntax: hexdec(hex_string)
substr() function returns a part of the string.
Note: If the start parameter is a negative number and length is less than or equal to start, length is 0.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert php ipv6 to ipv4. For more information, please follow other related articles on the PHP Chinese website!