Home > Backend Development > PHP Tutorial > PHP的ip2long和long二ip函数的实现原理

PHP的ip2long和long二ip函数的实现原理

WBOY
Release: 2016-06-13 12:00:48
Original
866 people have browsed it

PHP的ip2long和long2ip函数的实现原理

最近要做个十进制数字的可逆转换做邀请码,一直没搞清楚怎么弄的,实在太复杂了,今天弄IP时想到这个可以进行转换,于是研究了下原理:

主要是自己整理了下:


$ip = '12.34.56.78';$ips = explode('.', $ip);$result = 0;$result += $ips[0]';echo bindec(decbin(ip2long($ip)));echo '<br>';$str = '';$str .= intval($result/intval(pow(2, 24))) .'.';$str .= intval(($result&0x00FFFFFF)/intval(pow(2, 16))) .'.';$str .= intval(($result&0x0000FFFF)/intval(pow(2, 8))) .'.';$str .= intval($result&0x000000FF);echo $str;echo '<br>';echo long2ip($result);
Copy after login

以上输出的结果为:

20356923020356923012.34.56.7812.34.56.78
Copy after login


这只是实现的一种方法而已,还有其他的办法

我是想用类似的方法,可以直接把一个十进制数字转换为其他十进制数值并可逆,但遇到中间有很多0的数值时发现问题太多了,如 1000100 




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