search
Homephp教程php手册php中获取指定IP的物理地址的代码(正则表达式)

自己搭建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 "
    ";
    ?>



    这段代码采用访问http://www.ip138.com后使用正则表达式进行截取部分内容,其中正则表达式是自己写的。

    开始还好,下面的处理结果出现了乱码,如果把$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 "
    ";
    ?>



    结果还令人满意

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



    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use