| (.*?)<\/td> | (.*?)<\/td> | (.*?)<\/td><\/tr>/i', $html, $result);
        $update = array();
        if (isset($result[1]) && isset($result[2]) && isset($result[3])) {
            foreach ($result[3] as $k => $v) {
                $update['id'] = $this->id($result[1][$k], 12);
                $update['code'] = $result[2][$k];
                $update['name'] = strip_tags($v);
                $update['town_id'] = $town;
                $update['county_id'] = $county;
                $update['city_id'] = $city;
                $update['province_id'] = $province;
                $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
                $update = Dever::load('data', 'area')->pinyin($update);
                $this->up('village', $update['id'], $update);
            }
        }
    }
    public function id($id, $len = 6)
    {
        $id = substr($id, 0, $len);
        $id = str_pad($id, $len, '0', STR_PAD_RIGHT);
        return $id;
    }
    public function setLevelCounty(&$update)
    {
        $num = substr($update['id'], 4);
        # type = 1城区 2郊区 3县城 4经济技术开发 5县级市
        if ($update['name'] == '门头沟区') {
            $update['type'] = 2;
            $update['level'] = 2;
        } elseif ($num <= 10) {
            $update['type'] = 1;
            $update['level'] = 1;
        } elseif ($num > 10 && $num <= 20) {
            $update['type'] = 2;
            $update['level'] = 2;
        } elseif ($num > 20 && $num <= 70) {
            $update['type'] = 3;
            $update['level'] = 3;
        } elseif ($num > 70 && $num <= 80) {
            $update['type'] = 4;
            $update['level'] = 2;
        } elseif ($num >= 80) {
            $update['type'] = 5;
            $update['level'] = 2;
        }
    }
    public function up($table, $id, $data)
    {
        $db = Dever::db($table, 'area');
        $info = $db->find($id);
        if (!$info) {
            $db->insert($data);
        } else {
            $db->update($info['id'], $data);
        }
        return $id;
    }
    private function html($url)
    {
        $html = Dever::curl($url)->result();
        //$html = Dever::convert($html, "UTF-8", "GBK");
        $html = preg_replace('//', '', $html); // 去掉HTML注释
        $html = preg_replace('/\s+/', ' ', $html); // 清除多余的空格
        $html = preg_replace('/>\s', '><', $html); // 去掉标记之间的空格
        return $html;
    }
} |