<?php
function init(){
$d = include_once ('area.php');
$data = $d['list'];
foreach ($data as $k=>&$v){
if($v['level'] == 3 && $v['shortname'] =='平陆'){
$v['shortname'] ='平陆县';
}
if($v['level'] == 3 && $v['shortname'] =='资源'){
$v['shortname'] ='资源县';
}
if($v['level'] == 3 && $v['shortname'] =='河北'){
$v['shortname'] ='河北区';
}
if($v['level'] == 3 && $v['shortname'] =='运河'){
$v['shortname'] ='运河区';
}
}
return $data;
}
//过滤二维数组
function filterUniqueByKeys(array $array, array $keys) {
$uniqueItems = [];
$uniqueIdentifiers = [];
foreach ($array as $item) {
// 构建唯一标识符(基于提供的键)
$identifier = '';
foreach ($keys as $key) {
if (isset($item[$key])) {
$identifier .= $item[$key] . '-';
}
}
// 移除末尾的'-'(如果有的话)
$identifier = rtrim($identifier, '-');
// 检查标识符是否已存在
if (!in_array($identifier, $uniqueIdentifiers)) {
// 如果不存在,则添加到结果数组和唯一标识符数组中
$uniqueItems[] = $item;
$uniqueIdentifiers[] = $identifier;
}
}
return $uniqueItems;
}
function getarea($s=''){
$beifens=$s;//需要一个备份才可以
$temsheng = [];//因为有可能匹配到多个,所以需要用到数组
$temshi = [];//因为有可能匹配到多个,所以需要用到数组
$temquxian = [];//因为有可能匹配到多个,所以需要用到数组
$shengtem_index_f = 0;
$shitem_index_f = 0;
$quxiantem_index_f = 0;
$maparea = init();
$shengcount = 0;
$shicount = 0;
$quxiancount = 0;
foreach ($maparea as $k=>$v){
//省的循环与遍历
if($v['level'] == 1){
if(stripos($s,$v['name']) !== false ){
if(stripos($s,$v['name']) <= $shengtem_index_f || $shengcount == 0){
// echo stripos($s,$v['name']).$v['name']."<br>";
$shengtem_index_f = stripos($s,$v['name']);
}
$temsheng[] = $v;
$shengcount++;
}
//找到了 省级别
if(stripos($s,$v['shortname']) !== false ){
if(stripos($s,$v['shortname']) <= $shengtem_index_f || $shengcount == 0){
// echo stripos($s,$v['shortname']).$v['shortname']."<br>";
$shengtem_index_f = stripos($s,$v['shortname']);
}
$temsheng[] = $v;
$shengcount++;
}
}
//市的循环与遍历
if($v['level'] == 2){
if(stripos($s,$v['shortname']) !== false){
if(stripos($s,$v['shortname']) <= $shitem_index_f || $shicount==0){
$shitem_index_f = stripos($s,$v['shortname']);
}
$temshi[]=$v;
$shicount ++;
}
if(stripos($s,$v['name']) !== false){
if(stripos($s,$v['name']) <= $shitem_index_f || $shengcount == 0){
$shitem_index_f = stripos($s,$v['name']);
}
$temshi[]=$v;
$shicount ++;
}
}
//区县的循环与遍历
if($v['level'] == 3){
if(stripos($s,$v['shortname']) !== false){
if(stripos($s,$v['shortname']) <= $quxiantem_index_f || $quxiancount ==0){
$quxiantem_index_f = stripos($s,$v['shortname']);
}
$temquxian[]=$v;
$quxiancount ++;
}
if(stripos($s,$v['name']) !== false){
if(stripos($s,$v['name']) <= $quxiantem_index_f || $quxiancount ==0){
$quxiantem_index_f = stripos($s,$v['name']);
}
$temquxian[]=$v;
$quxiancount ++;
}
}
}
//保持二维数组的唯一性
$temsheng = filterUniqueByKeys($temsheng,['id']);
$temshi = filterUniqueByKeys($temshi,['id']);
$temquxian = filterUniqueByKeys($temquxian,['id']);
if(($shengtem_index_f > $shitem_index_f && $shicount >0) || ($shengtem_index_f > $quxiantem_index_f && $quxiancount >0)){
// echo "删除省";
$temsheng = [];
}
if($shitem_index_f > $quxiantem_index_f && $quxiancount > 0){
// echo "删除市";
$temshi = [];
}
$shengtem_index = 0;
$shitem_index = 0;
$quxiantem_index = 0;
$lastsheng = null;
$lastshi = null;
$lastquxian = null;
//省同级别按照出现的顺序获得优先级
foreach ($temsheng as $kk=>$vv){
if(stripos($beifens,$vv['name']) !== false ){
if(stripos($beifens,$vv['name']) <=$shengtem_index || ($shengtem_index == 0 && $kk == 0)){
$shengtem_index = stripos($beifens,$vv['name']);
$lastsheng = $vv;
}
}
if(stripos($beifens,$vv['shortname']) !== false ){
if(stripos($beifens,$vv['shortname']) <=$shengtem_index || ($shengtem_index == 0 && $kk == 0)){
$shengtem_index = stripos($beifens,$vv['shortname']);
$lastsheng = $vv;
}
}
}
//去掉不在这个省下面的市
if($lastsheng){
foreach ($temshi as $kk=>$vv){
if($vv['pid'] != $lastsheng['id']){
unset($temshi[$kk]);
}
}
}
sort($temshi);
//循环各个市
foreach ($temshi as $kk=>$vv){
if(stripos($beifens,$vv['name']) !== false ){
if(stripos($beifens,$vv['name']) <=$shitem_index || ($shitem_index == 0&& $kk ==0)){
$shitem_index = stripos($beifens,$vv['name']);
if($lastsheng){
if($lastsheng['id'] == $vv['pid']){
$lastshi = $vv;
}
}else{
$lastshi = $vv;
}
}
}
if(stripos($beifens,$vv['shortname']) !== false ){
if(stripos($beifens,$vv['shortname']) <=$shitem_index || ($shitem_index == 0&& $kk ==0) ){
$shitem_index = stripos($beifens,$vv['shortname']);
if($lastsheng){
if($lastsheng['id'] == $vv['pid']){
$lastshi = $vv;
}
}else{
$lastshi = $vv;
}
}
}
}
//去掉不在这个市下面的区县
if($lastshi){
foreach ($temquxian as $kk=>$vv){
if($vv['pid'] != $lastshi['id']){
unset($temquxian[$kk]);
}
}
}
//去掉不在这个省下面的区县
if($lastsheng && !$lastshi){
foreach ($temquxian as $kk=>$vv){
//找到市
$below = false;
foreach ($maparea as $k1=>$v1){
if($vv['pid'] == $v1['id'] && $v1['pid'] == $lastsheng['id']){
//这里计算要保留的区县
$below = true;
break;
}
}
if(!$below){
unset($temquxian[$kk]);
}
}
}
sort($temquxian);
foreach ($temquxian as $kk=>$vv){
if(stripos($beifens,$vv['name']) !== false ){
if(stripos($beifens,$vv['name']) <=$quxiantem_index || ($quxiantem_index == 0 && $kk==0)){
$quxiantem_index = stripos($beifens,$vv['name']);
if($lastshi){
if($lastshi['id'] == $vv['pid']){
$lastquxian = $vv;
}
}else{
$lastquxian = $vv;
}
}
}
if(stripos($beifens,$vv['shortname']) !== false ){
if(stripos($beifens,$vv['shortname']) <=$quxiantem_index || ($quxiantem_index == 0 && $kk==0)){
$quxiantem_index = stripos($beifens,$vv['shortname']);
if($lastshi){
if($lastshi['id'] == $vv['pid']){
$lastquxian = $vv;
}
}else{
$lastquxian = $vv;
}
}
}
}
//通过匹配的优先级进行计算
if($lastquxian && ($quxiantem_index<$shitem_index || $quxiantem_index < $shengtem_index)){
//区县最先找到。那么通过区县去找市省
//循环获得市和省
foreach ($maparea as $k=>$v){
if($lastquxian['pid'] == $v['id']){
$lastshi = $v;
break;
}
}
foreach ($maparea as $k=>$v){
if($lastshi['pid'] == $v['id']){
$lastsheng = $v;
break;
}
}
}
// print_r($lastshi);
if($lastshi && $shitem_index < $shengtem_index){
//市就找省
foreach ($maparea as $k=>$v){
if($lastshi['pid'] == $v['id']){
$lastsheng = $v;
break;
}
}
}
//1、只存在省,不存在地区市县市的 就不用考虑
if ($lastsheng && !$lastshi && !$lastquxian ){
//pass //先不考虑
}
//2、存在市,则需要找到省
if($lastshi){
if($lastsheng){
//检查省是否正常,否则就去掉市,因为省的优先级更高
if($lastshi['pid'] != $lastsheng['id']){
$lastshi=null;//省都不对,那就不要这个市了
$lastquxian=null;//城市都不对,区县也就不要了
}
}else{
//遍历获得省
foreach ($maparea as $k=>$v){
if($v['id']==$lastshi['pid']){
$lastsheng = $v;
break;
}
}
}
}
//3、存在县,需要找到市和省(需要再加一步骤,就是如果存在省,那么就需要判断省是否存在)
if($lastquxian){
if($lastshi){
//判断对不对,不对的话那就都清空
if($lastquxian['pid'] != $lastshi['id']){
$lastquxian=null;//城市都不对,区县也就不要了
//省市的优先级比区域高,这里就不能处理
}
}else{
//循环获得市和省
foreach ($maparea as $k=>$v){
if($lastquxian['pid'] == $v['id']){
$lastshi = $v;
break;
}
}
//如果不存在省,那就去便利获得,
if(!$lastsheng){
foreach ($maparea as $k=>$v){
if($v['id'] == $lastshi['pid'] && $v['level'] == 1){
$lastsheng = $v;
break;
}
}
}else{
//如果存在省,那就去对比成立不成立,不成立那么就清空省市
if($lastsheng['id'] != $lastshi['pid']){
$lastshi = null;
$lastquxian = null;
}
}
}
}
$sheng = '';
$shi = '';
$qu ='';
return ['sheng'=>$lastsheng,'shi'=>$lastshi,'qu'=>$lastquxian];
}
$string = '富川瑶族自治县山塘维修基础设施建设项目';
$sr = getarea($string);
print_r($sr);
通过文本识别省市区完整算法PHP版本
于 2024-08-14 23:53:01 首次发布