Home  >  Article  >  Backend Development  >  PHP code that converts IP addresses and numbers to each other

PHP code that converts IP addresses and numbers to each other

WBOY
WBOYOriginal
2016-07-25 09:03:551301browse
  1. $ip = "182.18.8.196";

  2. echo '
    ';
  3. echo ip2long($ip);
  4. echo " @ ";
  5. echo $ipfu = ip2int($ ip);
  6. echo ' @ ';
  7. $ip_n = bindec(decbin(ip2long($ip)));
  8. echo $ip_n;
  9. echo ' @ ';
  10. echo long2ip($ip_n);
  11. echo ' @ ';
  12. echo long2ip($ipfu);//Convert numbers to IP

  13. //You can directly use this function to get the number converted from the IP address.

  14. function myip2long($ip){
  15. return bindec(decbin(ip2long($ip)));
  16. }

  17. //This function is equivalent to ip2long

  18. function ip2int($ip){
  19. list($ip1,$ip2,$ip3,$ip4)=explode(".",$ip);
  20. return ($ip1<<24)|($ip2<<16)|($ip3<< ;8)|($ip4);
  21. }

Copy the code

This way you can get the correct result: -1240332092 @ -1240332092 @ 3054635204 @ 182.18.8.196 @ 182.18.8.196 3054635204

Directly call the myip2long function to get a positive number. Articles you may be interested in: php code to realize IP address and number exchange



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