1 <?php
2 /*
3 * 解析国家行政区域的类,s使用方法为使用文件地址实例化然后调用getResult()方法即可,获取数组形式的数据
4 */
5 class AnalyRegion {
6
7 public $file;
8 public $results = array();
9
10 private $handle;
11
12 public function __construct($file){
13 $this->file = $file;
14 }
15
16 public function getResult(){
17 echo $this->file;
18 $this->handle = fopen($this->file,'r');
19 while (!feof($this->handle)) {
20 $row = trim(fgets($this->handle));
21 if (!preg_match('/^(\d+)(\s+)(.+)$/', $row, $matches)) {
22 continue;
23 }
24 list($row, $id, $delimiter, $regionName) = $matches;
25 $record['id'] = $id;
26 $record['region_name'] = $regionName;
27 $record['parent_id'] = $this->getParentId($id);
28 $this->results[] = $record;
29 }
30 return $this->results;
31 }
32
33 /*
34 * 解析Id
35 */
36 private function getParentId($id){
37 $level;
38 $id = (int)$id;
39 if(($id % 10000) == 0){ //如果后4位为0那么就是省级区域
40 $level = 0;
41 $parent_id = 0; //父ID为0
42 }else if(($id % 100) == 0){ //如果后两位为0就是市级区域父id取整前2位
43 $parent_id = floor($id/10000)*10000;
44 }else{
45 $parent_id = floor($id/100)*100; //最后就是下面一级了
46 }
47 return $parent_id;
48 }
49
50 //判断是否是utf8编码
51 private function is_utf8($liehuo_net){
52 if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$liehuo_net) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$liehuo_net) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$liehuo_net) == true){
53 return true;
54 }else{
55 return false;
56 }
57 }
58 }
59 ?>
数据在国家统计网站上有,有什么不对的地方恳求指正!