复制代码 代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8" />
<meta content="" />
<meta content="" />
<meta content="" />
</head>
<body>
<?php
$ipAddr = "218.75.124.100";
$ip138Addr = "http://www.ip138.com/ips8.asp?ip=".$ipAddr."&action=2";
$contents = file_get_contents($ip138Addr);
$pattern = "//<li.+?//li/>/";
$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 = "<li>本站主数据:</li>";break;
case 1:$str = "<li>参考数据一:</li>";break;
case 2:$str = "<li>参考数据二:</li>";break;
case 3:$str = "<li>参考数据三:</li>";break;
case 4:$str = "<li>参考数据四:</li>";break;
case 5:$str = "<li>参考数据六:</li>";break;
case 6:$str = "<li>参考数据七:</li>";break;
}
$addrArray[0][$i] = trim($addrArray[0][$i],$str);
}
$addrArray[0][0] = trim($addrArray[0][0],":");
echo "<pre>";
print_r($addrArray);
echo "</pre>";
?>
</body>
</html>
复制代码 代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8" />
<meta content="" />
<meta content="" />
<meta content="" />
</head>
<body>
<?php
$ipAddr = "4.4.44.4";
$ipChinazAddr = "http://tool.chinaz.com/IP/?IP=".$ipAddr;
$contents = file_get_contents($ipChinazAddr);
$pattern = "//=/=/>/>.+/<//strong/>/<br ///>/";
$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 "<pre>";
print_r($addrArray);
echo "</pre>";
?>
</body>
</html>
以上是本人写的两个获取IP物理地址的代码,作为一个初学者,没事的时候写写简单的代码也还是一件挺有意思的事。但是毕竟是一个初学者,不能更好的注意安全性、空间利用率、时间效率等问题,还望各位留下你的足迹,小弟谢了
新闻热点
疑难解答