Home >Backend Development >PHP Problem >How to convert php ipv6 to ipv4

How to convert php ipv6 to ipv4

藏色散人
藏色散人Original
2021-09-20 11:06:557556browse

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.

How to convert php ipv6 to ipv4

#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn