PHP code that converts IP addresses and numbers to each other

Source: Internet
Author: User
    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);//Digital conversion to IP

    13. This function can be used directly to derive the number of IP address conversion.

    14. function Myip2long ($IP) {
    15. Return Bindec (Decbin (Ip2long ($IP)));
    16. }

    17. This function and Ip2long are equivalent

    18. function Ip2int ($IP) {
    19. List ($ip 1, $ip 2, $ip 3, $ip 4) =explode (".", $ip);
    20. Return ($ip 1<<24) | ($ip 2<<16) | ($ip 3<<8) | ($ip 4);
    21. }

Copy Code

This can be positive:-1240332092 @-1240332092 @ 3054635204 @ 182.18.8.196 @ 182.18.8.1963054635204

Call the Myip2long function directly to get a positive number. You may be interested in the article: PHP implementation of IP address and digital interchange code

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.