Rumah  >  Artikel  >  php教程  >  PHP全国省市区地址分割提取脚本程序

PHP全国省市区地址分割提取脚本程序

WBOY
WBOYasal
2016-06-06 19:44:143882semak imbas

github地址:https://github.com/zmxfree/addressapart 比如将 浙江省杭州市江干区XX路X号 分割成 浙江省 杭州市 江干区 XX路X号 ,方便excel操作。 目前支持大部分中国省市区地址信息的分割,可以自行更新地址源信息。 主程序代码: ? php /* *- * 省市区地

github地址: https://github.com/zmxfree/addressapart

比如将 浙江省杭州市江干区XX路X号 分割成 浙江省 杭州市 江干区 XX路X号,方便excel操作。

目前支持大部分中国省市区地址信息的分割,可以自行更新地址源信息。

主程序代码:

<span>php
</span><span>/*</span><span>*-
 * 省市区地址分割
 * 将 浙江省杭州市江干区XX路X号 分割成 浙江省 杭州市 江干区 XX路X号
 * User: zmxfree@gmail.com
 * Date: 14-6-18
 * Time: 下午12:52
 </span><span>*/</span>

<span>/*</span><span>*此引入文件应包含全国所有的省市区信息
 * 文件格式为多维数组,键是地名,值可以赋一个编号,供回查
 * array(
 * [0] => array('浙江省' => '1','北京市' => '2','上海市' => '3',...),
 * [1] => array('杭州市' => '1-1','宁波市' => '1-2','市辖区(一般的直辖市会分为市辖区和周边地区)' => '2-1,3-1',...),
 * [3] => array('西湖区'  => '1-1-1','江干区' => '1-1-2','海淀区'=>'2-1-1',...),
 * [4] => array('可按需求添加城镇信息')
 * )
 * 这样的结构CRUD操作很方便,不需要严格按照省市区结构分配。
 </span><span>*/</span>
<span>$address</span> = <span>include</span>('address.info.php'<span>);
</span><span>$cache_file</span> = 'lenarr.cache.php'<span>;
mb_internal_encoding(</span>'utf8'<span>);
</span><span>$len_arr</span> = <span>array</span><span>();
</span><span>if</span> (<span>is_file</span>(<span>$cache_file</span>) && <span>is_readable</span>(<span>$cache_file</span><span>)) {
    </span><span>//</span><span>读取省市区长度缓存</span>
    <span>$len_arr</span> = <span>include</span>(<span>$cache_file</span><span>);
} </span><span>else</span><span> {
    </span><span>$fp</span> = <span>fopen</span>(<span>$cache_file</span>, 'w'<span>);
    </span><span>//</span><span>省市区的数量有很多,但长度却是有限的,直接计算出长度,用长度去匹配,大大减少匹配次数</span>
    <span>$len_arr</span>[] = <span>array_values</span>(<span>array_unique</span>(<span>array_map</span>('mb_strlen', <span>array_keys</span>(<span>$address</span>[0])))); <span>//</span><span>所有省的长度</span>
    <span>$len_arr</span>[] = <span>array_values</span>(<span>array_unique</span>(<span>array_map</span>('mb_strlen', <span>array_keys</span>(<span>$address</span>[1])))); <span>//</span><span>市的长度</span>
    <span>$len_arr</span>[] = <span>array_values</span>(<span>array_unique</span>(<span>array_map</span>('mb_strlen', <span>array_keys</span>(<span>$address</span>[2])))); <span>//</span><span>区的长度
    //保存文件缓存</span>
    <span>$result</span> = <span>fwrite</span>(<span>$fp</span>, '<?php return ' . <span>var_export(<span>$len_arr</span>, <span>true</span>) . ';?>'<span>);
    </span><span>fclose</span>(<span>$fp</span><span>);
}

</span><span>//</span><span>读取要分割的地址</span>
<span>$f</span> = './address.log'<span>;
</span><span>$fstr</span> = <span>is_file</span>(<span>$f</span>) && <span>is_readable</span>(<span>$f</span>) ? <span>file_get_contents</span>(<span>$f</span>) : ''<span>;
</span><span>$add_arr</span> = <span>explode</span>("\n", <span>$fstr</span><span>);

</span><span>if</span> (<span>is_array</span>(<span>$add_arr</span><span>)) {
    </span><span>foreach</span> (<span>$add_arr</span> <span>as</span> <span>$addr</span><span>) {
        </span><span>//</span><span>初始化</span>
        <span>$l</span> = 0<span>;
        </span><span>$i</span> = 0<span>;
        </span><span>$p</span> = 0<span>;
        </span><span>$find</span> = <span>false</span><span>;
        </span><span>$arr_get</span> = <span>array</span><span>();
        </span><span>$addr</span> = <span>trim</span>(<span>$addr</span><span>);

        </span><span>while</span> (!<span>$find</span><span>) {
            </span><span>//</span><span>判断是否超出lenarr数组的长度</span>
            <span>if</span> (!<span>isset</span>(<span>$len_arr</span>[<span>$l</span><span>])) {
                </span><span>$arr_get</span>[] = mb_substr(<span>$addr</span>, <span>$p</span>, <span>null</span><span>);
                </span><span>$find</span> = <span>true</span><span>;
                </span><span>break</span><span>;
            }

            </span><span>//</span><span>截取地址</span>
            <span>$ad</span> = mb_substr(<span>$addr</span>, <span>$p</span>, <span>$len_arr</span>[<span>$l</span>][<span>$i</span><span>]);
            </span><span>//</span><span>匹配,匹配到就进入下一层级即$l++</span>
            <span>if</span> (<span>isset</span>(<span>$address</span>[<span>$l</span>][<span>$ad</span><span>])) {
                </span><span>$arr_get</span>[] = <span>$ad</span>; <span>//</span><span>存储值</span>
                <span>$p</span> += <span>$len_arr</span>[<span>$l</span>][<span>$i</span><span>];
                </span><span>$i</span> = 0<span>;
                </span><span>$l</span>++<span>;
                </span><span>continue</span><span>;
            }
            </span><span>$i</span>++<span>;

            </span><span>//</span><span>判断当前层级是否循环完毕
            //当前层级循环完毕仍未匹配到,则循环下一层级,一般是直辖市比如北京市海淀区这种情况,或者是信息不全</span>
            <span>if</span> (<span>isset</span>(<span>$len_arr</span>[<span>$l</span>]) && <span>$i</span> >= <span>count</span>(<span>$len_arr</span>[<span>$l</span><span>])) {
                </span><span>echo</span> <span>$ad</span> . '<br>'; <span>//</span><span>记录下来</span>
                <span>$i</span> = 0<span>;
                </span><span>$l</span>++<span>;
                </span><span>continue</span><span>;
            }
        }
        </span><span>//</span><span>分割好的地址写入文件</span>
        <span>file_put_contents</span>('address.detail', <span>implode</span>("\t", <span>$arr_get</span>) . "\n",<span> FILE_APPEND);
    }
}</span>

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn