Code to get the physical address of the specified IP in PHP (regular expression) _php tutorial

Source: Internet
Author: User
Build your own IP database for resources, and update inconvenience, why not use off-the-shelf IP query? Below yourself wrote a Get IP physical address of the PHP code (there is a certain flaw, please enlighten the master)
Copy CodeThe code is as follows:



<title></title>






$IPADDR = "218.75.124.100";
$ip 138Addr = "http://www.ip138.com/ips8.asp?ip=". $ipAddr. " &action=2 ";
$contents = file_get_contents ($ip 138Addr);
$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 data of the site:
  • "; break;
    Case 1: $str = "
  • Reference Data One:
  • "; break;
    Case 2: $str = "
  • Reference Data Two:
  • "; break;
    Case 3: $str = "
  • Reference Data three:
  • "; break;
    Case 4: $str = "
  • Reference Data four:
  • "; break;
    Case 5: $str = "
  • Reference Data VI:
  • "; break;
    Case 6: $str = "
  • Reference Data VII:
  • "; break;
    }
    $addrArray [0][$i] = Trim ($addrArray [0][$i], $str);
    }
    $addrArray [0][0] = Trim ($addrArray [0][0], ":");
    echo "


    echo "
    ";
    ?>



    This code uses a regular expression to intercept portions of the content after accessing http://www.ip138.com, where the regular expression is written by itself.

    Beginning fortunately, the following processing results appear garbled, if the $str (remove the string id) definition of the string is not garbled, and add a separate ":" Statement, you can achieve the phenomenon of no garbled, do not know why ...
    Regular Expression--gets the physical address of the specified IP (ii) CHINAZ data
    The previous IP obtained the result is not very perfect, the reason is because ip138 IP resolution address foreign addresses is not very detailed, so re-write a code with chianz.com.
    Copy CodeThe code is as follows:



    <title></title>






    $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 "


    echo "
    ";
    ?>



    The results are also satisfactory.

    The above is I wrote two to obtain the IP physical address of the code, as a beginner, when it's okay to write simple code is also a very interesting thing. But after all, is a beginner, can not better pay attention to safety, space utilization, time efficiency and other issues, but also hope you leave your footprints, brother Xie

    http://www.bkjia.com/PHPjc/323668.html www.bkjia.com true http://www.bkjia.com/PHPjc/323668.html techarticle build your own IP database for resources, and update inconvenience, why not use off-the-shelf IP query? Below yourself wrote a Get IP physical address of the PHP code (there is a certain flaw, please do not hesitate to master ...

  • Related Article

    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.