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

How to convert php ipv6 to ipv4

藏色散人
Release: 2023-03-12 19:24:01
Original
7564 people have browsed it

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template