ip转十进制(PHP、MySQL)

mysql与php中都提供了IP转换十进制数函数

 

1. IP 由点分格式,转换为数字格式,代码如下:

  1. mysql> select inet_aton('127.0.0.1'); 
  2. +------------------------+ 
  3. | inet_aton('127.0.0.1') | 
  4. +------------------------+ 
  5. | 2130706433 |  
  6. +------------------------+ 
  7. 1 row in set (0.00 sec) 

2. IP 由数字格式转换为点分格式,代码如下:

  1. mysql> select inet_ntoa(2130706433); 
  2. +-----------------------+ 
  3. | inet_ntoa(2130706433) | 
  4. +-----------------------+ 
  5. | 127.0.0.1 |  
  6. +-----------------------+ 
  7. 1 row in set (0.00 sec) 

 

php可直接使用ip2long函数,代码如下:

  1. echo ip2long('192.168.1.38'); 
  2. //输出:3232235814 
posted @ 2017-09-07 18:07  邀魂斩月  阅读(732)  评论(0编辑  收藏  举报