中国各地区的IP段解析

    public static function ip()
    {
        $file = self::arguments('file');
        if (!file_exists($file)) {
            Error::terminal('invalid file.');
        }

        static $citys;
        $region = '';
        $match = array();

        $provinces = array_flip(Usr::findProvince());

        $handle = @fopen($file, 'rb');
        while (!feof($handle)) {

            if (!$buffer = trim(fgets($handle))) {
                continue;
            }

            $line = explode(' ', $buffer);
            $line = array_values(array_filter($line));

            if (!isset($line['2'])) {
                continue;
            }

            $province = mb_substr($line['2'], 0, 2, 'UTF-8');
            if (!($match = preg_grep("/{$province}/i", $provinces))) {
                continue;
            }

            $region = array_rand($match, 1);
            $city   = trim(mb_substr($line[2], mb_strlen($match[$region], 'UTF-8'), 3, 'UTF-8'), '()./\\');

            $output = $match[$region];

            if (!isset($citys[$region])) {
                $citys[$region] = array_flip(Usr::findCity($region));
            }

            if ($match = preg_grep("/{$city}/i", $citys[$region])) {
                $region = array_rand($match, 1);
                $output .= " " . $match[$region];
            }

            $ip = sprintf('%u', ip2long($line['0'])) . ',' . sprintf('%u', ip2long($line['1']));
            $output .= " " . $ip;

            self::output($output);
            Usr::addRegionip($region, $ip);
        }

        self::output('OK!');
        fclose($handle);
    }

    public static function china()
    {
        $file = self::arguments('file');
        if (!file_exists($file)) {
            Error::terminal('invalid file.');
        }

        $china = array();
        $handle = @fopen($file, 'rb');
        while (!feof($handle)) {

            if (!$buffer = trim(fgets($handle))) {
                continue;
            }

            $line = explode(' ', $buffer);
            $line = array_values(array_filter($line));
            $china[$line[0]] = $line[1];
        }

        foreach ($china as $pid => $pname) {
            if (!preg_match("/\d{2}0{4}/", $pid)) {
                continue;
            }

            Usr::addProvince($pid, $pname);

            foreach ($china as $cid => $cname) {
                $filter = substr($pid, 0 ,2);
                if (!preg_match("/{$filter}\d{4}/", $cid) || preg_match("/{$filter}0{4}/", $cid)) {
                    continue;
                }

                if (in_array($cname, array('市辖区', '县'))) {
                    continue;
                }

                if (in_array($pname, array('北京市', '天津市', '上海市', '重庆市')) 
                    || preg_match("/{$filter}[0-9]{2}0{2}/", $cid)) {
                    Usr::addCity($pid, $cid, $cname);
                    $output = $pname . " " . $pid . " " . $cname . " " . $cid;
                    self::output($output);
                }
            }
        }

        self::output('OK!');
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值