Home > Article > Backend Development > How to convert ipv6 to ipv4 in php
php method to convert ipv6 to ipv4: first create a PHP sample file; then define an ipv6 address; finally convert the ipv6 address to ipv4 address through the hexdec and substr functions.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
How does php convert ipv6 to ipv4?
php code converts ipv6 address to ipv4 address
$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;
Related introduction:
hexdec() Function converts hexadecimal is decimal.
Syntax
hexdec(hex_string)
Parameters
hex_string Required. Specifies the hexadecimal number to be converted.
Description
Returns the decimal number equivalent to the hexadecimal number represented by the hex_string parameter. hexdec() Converts a hexadecimal string to a decimal number. The maximum value that can be converted is 7ffffffff, which is 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers, in which case it returns a float type.
hexdec() Replaces all non-hexadecimal characters encountered with 0. This way, all zeros on the left are ignored, but zeros on the right are included in the value.
substr() The function returns a part of the string.
Note: If the start parameter is negative and length is less than or equal to start, length is 0.
Grammar
substr(string,start,length)
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to convert ipv6 to ipv4 in php. For more information, please follow other related articles on the PHP Chinese website!