2 danzi638 danzi638 于 2017.09.12 22:26 提问

请问PHP怎么把一段ip地址分解成一个数组

如题:
比如说我输入0.0.0.0,然后会把0.0.0.0分解成4段数据都为0.求解下这个要怎么实现,谢谢。

3个回答

caozhy
caozhy   Ds   Rxr 2017.09.13 08:30
katelyn9
katelyn9   2017.09.13 15:09

$ip = "0.0.0.0";
$arr = explode(".",$ip);
$arr就是你要的数组

danzi638
danzi638   2018.03.02 11:43

ip查询系统,满意可以赞同下。
<!DOCTYPE html>



ip查询系统

<?php
$conn = mysqli_connect('localhost:3308', 'root', '', 'myapp');
mysqli_set_charset($conn, "utf8");
if ($conn) {
$ip_result = null;
$ip = null;
$iparr = null;
$result = mysqli_query($conn, "SELECT * FROM ipsearch");
$data_count = mysqli_num_rows($result);
for ($i = 0; $i < $data_count; $i++) {
$result_arr = mysqli_fetch_assoc($result);
$startip = $result_arr['startip'];
$endip = $result_arr['endip'];
$startiparr = explode(".", $startip);
$endiparr = explode(".", $endip);

    if (empty($_GET["ip"])) {
        die("输入ip进行查询");
    } else {
        $ip = $_GET["ip"];
        $iparr = explode(".", $ip);
        if (count($iparr) == 4 && $iparr[0] <= 255 && $iparr[1] <= 255 && $iparr[2] <= 255 && $iparr[3] <= 255) {
            for ($j = 0; $j < 4; $j++) {
                $compare_one[$j] = intval($endiparr[$j]) - intval($startiparr[$j]);
            }
            for ($k = 0; $k < 4; $k++) {
                $compare_two[$k] = intval($iparr[$k]) - intval($startiparr[$k]);
            }
            if ($compare_two[0] <= $compare_one[0]) {
                if ($compare_two[1] <= $compare_one[1]) {
                    if ($compare_two[2] <= $compare_one[2]) {
                        if ($compare_two[3] <= $compare_one[3]) {
                            $ip_result = $startip;
                            $location = mysqli_query($conn, "SELECT * FROM ipsearch WHERE startip = '$ip_result'");
                            $locationarr = mysqli_fetch_assoc($location);
                            echo "您输入的ip地址为:" . $ip . '<br>';
                            echo '查询到您所在的位置为:' . $locationarr['location'];
                            break;
                        }
                    }
                }
            }
        }else{
            die('输入ip地址有错请重新输入');
            break;
        }
    }

// echo "

$id$startip$endip$location";
}

} else {
echo '连接失败';
}
?>



Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!