May 18
作者: 肖建彬 | 可以转载, 转载时务必以超链接形式标明文章原始出处和作者信息及版权声明
网址:http://www.xiaojb.com/archives/it/ip2long.shtml
网址:http://www.xiaojb.com/archives/it/ip2long.shtml
在php中,有ip2long,long2ip进行字符串IP地址和网络地址的转换。
$long = ip2long(”127.0.0.1″);
$ip = long2ip(42949672);
php4和php5有一些不同,php4中对无效的IP地址返回-1,而php5中是返回FALSE。
mysql也内置了函数做这些转换。INET_ATON和INET_NTOA。
mysql> select INET_ATON("adfsafsafsf");
+--------------------------+
| INET_ATON("adfsafsafsf") |
+--------------------------+
| NULL |
+--------------------------+
1 row in set (0.00 sec)
mysql> select INET_ATON("255.255.255.255");
+------------------------------+
| INET_ATON("255.255.255.255") |
+------------------------------+
| 4294967295 |
+------------------------------+
1 row in set (0.00 sec)
mysql> select INET_NTOA("4294967295");
+-------------------------+
| INET_NTOA("4294967295") |
+-------------------------+
| 255.255.255.255 |
+-------------------------+
1 row in set (0.00 sec)
mysql> select INET_NTOA("42949672");
+-----------------------+
| INET_NTOA("42949672") |
+-----------------------+
| 2.143.92.40 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select INET_NTOA("429");
+------------------+
| INET_NTOA("429") |
+------------------+
| 0.0.1.173 |
+------------------+
1 row in set (0.00 sec)
mysql> select INET_NTOA("sfdasfdsdf");
+-------------------------+
| INET_NTOA("sfdasfdsdf") |
+-------------------------+
| 0.0.0.0 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)
mysql>
php中调用了inet_addr,ntohl,htonl,inet_ntoa等函数.

Recent Comments