Home Backend Development PHP Tutorial php中获取指定IP的物理地址的代码(正则表达式)_PHP

php中获取指定IP的物理地址的代码(正则表达式)_PHP

Jun 01, 2016 pm 12:15 PM
ip physical address

正则表达式

自己搭建IP数据库占资源,而且更新不便,何不使用现成的IP查询呢?下面自己写了个获取IP物理地址的PHP代码(有一定的瑕疵,请高手不吝赐教)
复制代码 代码如下:










$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;$iecho $addrArray[0][$i] = mb_convert_encoding($addrArray[0][$i],"UTF-8", "GB2312");
switch($i) {
case 0:$str = "
  • 本站主数据:
  • ";break;
    case 1:$str = "
  • 参考数据一:
  • ";break;
    case 2:$str = "
  • 参考数据二:
  • ";break;
    case 3:$str = "
  • 参考数据三:
  • ";break;
    case 4:$str = "
  • 参考数据四:
  • ";break;
    case 5:$str = "
  • 参考数据六:
  • ";break;
    case 6:$str = "
  • 参考数据七:
  • ";break;
    }
    $addrArray[0][$i] = trim($addrArray[0][$i],$str);
    }
    $addrArray[0][0] = trim($addrArray[0][0],":");
    echo "
    "; <br>print_r($addrArray); <br>echo "
    Copy after login
    Copy after login
    ";
    ?>



    这段代码采用访问http://www.ip138.com后使用正则表达式进行截取部分内容,其中正则表达式是自己写的。
    php中获取指定IP的物理地址的代码(正则表达式)_PHP
    开始还好,下面的处理结果出现了乱码,如果把$str(去除字符串首尾的标识)定义的字符串就没有了乱码;而且再加一个单独的去除“:”的语句,就可以实现无乱码的现象,不知道为什么了……
    正则表达式——获取指定IP的物理地址(二) chinaz数据
    上一个IP获取的结果不是很完善,究其原因是因为ip138的IP解析应对国外的地址时不是很详细,所以又重新写了个用Chianz.com的代码。
    复制代码 代码如下:










    $ipAddr = "4.4.44.4";
    $ipChinazAddr = "http://tool.chinaz.com/IP/?IP=".$ipAddr;
    $contents = file_get_contents($ipChinazAddr);
    $pattern = "/\=\=\>\>.+\\
    /";
    $string = $contents;
    preg_match_all($pattern,$string,$addrArray);
    $num = count($addrArray[0]);
    $pattern = "/(==>>).+(==>>\s)/";
    for($i = 0;$i$addrArray[0][$i] = preg_replace($pattern,"",$addrArray[0][$i]);
    }
    echo "
    "; <br>print_r($addrArray); <br>echo "
    Copy after login
    Copy after login
    ";
    ?>



    结果还令人满意

    php中获取指定IP的物理地址的代码(正则表达式)_PHP

    php中获取指定IP的物理地址的代码(正则表达式)_PHP

      以上是本人写的两个获取IP物理地址的代码,作为一个初学者,没事的时候写写简单的代码也还是一件挺有意思的事。但是毕竟是一个初学者,不能更好的注意安全性、空间利用率、时间效率等问题,还望各位留下你的足迹,小弟谢了

    Statement of this Website
    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

    Hot Article Tags

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What does binding ip and mac mean? What does binding ip and mac mean? Mar 09, 2023 pm 04:44 PM

    What does binding ip and mac mean?

    How do websites set black/whitelist IP restrictions and country and city IP access restrictions through nginx? How do websites set black/whitelist IP restrictions and country and city IP access restrictions through nginx? Jun 01, 2023 pm 05:27 PM

    How do websites set black/whitelist IP restrictions and country and city IP access restrictions through nginx?

    How to check IP address on WeChat How to check IP address on WeChat May 31, 2023 am 09:16 AM

    How to check IP address on WeChat

    How to set directory whitelist and ip whitelist in nginx How to set directory whitelist and ip whitelist in nginx May 18, 2023 pm 03:52 PM

    How to set directory whitelist and ip whitelist in nginx

    How to obtain the user's real IP address based on nginx reverse proxy How to obtain the user's real IP address based on nginx reverse proxy May 13, 2023 pm 05:07 PM

    How to obtain the user's real IP address based on nginx reverse proxy

    Local IP address query Local IP address query Jan 05, 2024 pm 01:42 PM

    Local IP address query

    What should I do if my wifi shows no IP allocation? What should I do if my wifi shows no IP allocation? Aug 30, 2023 am 11:58 AM

    What should I do if my wifi shows no IP allocation?

    What does IP mean? What does IP mean? Jan 05, 2024 pm 01:34 PM

    What does IP mean?

    See all articles