Php code for obtaining the physical address of the specified IP address (regular expression)

Source: Internet
Author: User
Common website modules may include registration, login, posting, comments, and comments. The most important thing is to obtain IP addresses and process data. If you build your own IP database to occupy resources and the update is inconvenient, why not use a ready-made IP address for query? Below I wrote a PHP code for obtaining the IP address's physical address (please kindly advise if you have any flaws)
Copy codeThe code is as follows:










$ IpAddr = "218.75.124.100 ";
$ Ip138Addr = "http://www.ip138.com/ips8.asp? Ip = ". $ ipAddr." & action = 2 ";
$ Contents = file_get_contents ($ ip138Addr );
$ Pattern = "/\ /";
$ String = $ contents;
Preg_match_all ($ pattern, $ string, $ addrArray );
$ Num = count ($ addrArray [0]);
For ($ I = 0; $ I <$ num; $ I ++ ){
Echo $ addrArray [0] [$ I] = mb_convert_encoding ($ addrArray [0] [$ I], "UTF-8", "GB2312 ");
Switch ($ I ){
Case 0: $ str ="
  • Main site data:
  • "; Break;
    Case 1: $ str ="
  • Reference Data 1:
  • "; Break;
    Case 2: $ str ="
  • Reference Data 2:
  • "; Break;
    Case 3: $ str ="
  • Reference Data 3:
  • "; Break;
    Case 4: $ str ="
  • Reference Data 4:
  • "; Break;
    Case 5: $ str ="
  • Reference Data 6:
  • "; Break;
    Case 6: $ str ="
  • Reference Data 7:
  • "; Break;
    }
    $ AddrArray [0] [$ I] = trim ($ addrArray [0] [$ I], $ str );
    }
    $ AddrArray [0] [0] = trim ($ addrArray [0] [0], ":");
    Echo"
    "; 
    print_r($addrArray);
    echo "
    ";
    ?>



    This code uses access to the http://www.ip138.com using a regular expression to intercept part of the content, where the regular expression is self-written.

    Fortunately, the following processing results contain garbled characters. if $ str (the first and last character of the string is removed) is defined, no garbled characters are found. In addition, a separate ": "statement, you can achieve the phenomenon of no garbled, do not know why ......
    Regular expression -- obtain the physical address of the specified IP address (2) chinaz data
    The result obtained from the previous IP address is not perfect. The reason is that the IP address resolution of ip138 is not very detailed in dealing with foreign addresses, so I re-wrote a code using Chianz.com.
    Copy codeThe code is as follows:










    $ IpAddr = "4.4.44.4 ";
    $ IpChinazAddr = "http://tool.chinaz.com/IP? IP = ". $ ipAddr;
    $ Contents = file_get_contents ($ ipChinazAddr );
    $ Pattern = "/\=\\>\>. + \ <\/strong \> \
    /";
    $ String = $ contents;
    Preg_match_all ($ pattern, $ string, $ addrArray );
    $ Num = count ($ addrArray [0]);
    $ Pattern = "/(==>>). + (==>>> \ s )/";
    For ($ I = 0; $ I <$ num; $ I ++ ){
    $ AddrArray [0] [$ I] = preg_replace ($ pattern, "", $ addrArray [0] [$ I]);
    }
    Echo"
    "; 
    print_r($addrArray);
    echo "
    ";
    ?>



    Satisfactory Results

    The above are two codes written by myself to obtain the physical IP address. as a beginner, it is quite interesting to write simple code when it is okay. After all, I am a beginner and cannot pay more attention to security, space utilization, time efficiency, and other issues. I hope you can leave your footprints here. thank you, younger brother.

    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.