php获取邮政编码的第一部分

编程入门 行业动态 更新时间:2024-10-08 20:30:57
本文介绍了php获取邮政编码的第一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用数据库提取搜索计算的邮政编码的第一部分。有任何想法吗?!我需要这个在网站中实现一个搜索模块,它根据邮政编码寻找一些相关信息。但我的邮政编码数据库,它没有第二部分的邮政编码,只有第一部分是有。它的英国邮政编码。

解决方案

这不会覆盖所有可能的英国邮政编码的100%,但会像99.999% :

/ ** * @param string $ postcode英国邮政编码 * @return string * / function getUKPostcodeFirstPart($ postcode) { //验证输入参数 $ postcode = strtoupper($ postcode); //英国大陆/海峡群岛(简化版本,因为我们不需要验证它) if(preg_match('/ ^ [AZ]([AZ]?\d (\ d | [AZ])?| \d [AZ]?)\s *?\d [AZ] [AZ] $ / i',$ postcode)) return preg_replace / ^([AZ]([AZ]?\d(\d | [AZ])?| \d [AZ]?))\s *?(\d [AZ] [AZ]) $ / i','$ 1',$ postcode); //英语强制 if(preg_match('/ ^(BFPO)\s *?(\d {1,4})$ / i',$ postcode)) return preg_replace('/ ^(BFPO)\s *?(\d {1,4})$ / i','$ 1',$ postcode); //海外领地 if(preg_match('/ ^(ASCN | BBND | BIQQ | FIQQ | PCRN | SIQQ | STHL | TDCU | TKCA)\s *?(1ZZ)$ / i' ,$ postcode)) return preg_replace('/ ^([AZ] {4})\s *?(1ZZ)$ / i','$ 1',$ postcode) //好...甚至其他形式的邮政编码...返回它是 return $ postcode; }

测试数据:

echo'TW135BQ - > ',getUKPostcodeFirstPart('TW135BQ'),\\\; echo'gir0aa - > ',getUKPostcodeFirstPart('gir0aa'),\\\; echo'RG5 3SQ - > ',getUKPostcodeFirstPart('RG5 3SQ'),\\\; echo'AB51 0GQ - > ',getUKPostcodeFirstPart('AB51 0GQ'),\\\; echo'YO104EA - > ',getUKPostcodeFirstPart('YO104EA'),\\\; echo'SE154NS - > ',getUKPostcodeFirstPart('SE154NS'),\\\; echo'W1B4BD - > ',getUKPostcodeFirstPart('W1B4BD'),\\\;

结果:

TW135BQ - > TW13 gir0aa - > GIR0AA RG5 3SQ - > RG5 AB51 0GQ - > AB51 YO104EA - > YO10 SE154NS - > SE15 W1B4BD - > W1B

请注意:您有责任提供有效的邮政编码。 / p>

编辑:oops,它不包括GIR 0AA,对不起

I need to extract the first part of postcode for search calculation using a database. Any ideas?! I need this to implement a search module in the site, which looks out for some relevant information according to the post code. But the database which I am having for post code, it doesnt have second part of the postcode, only the first part is there. Its for UK postcode. So, any ideas?!

解决方案

This will not cover 100% of all possible UK postcodes, but will do for like 99.999% or so :)

/** * @param string $postcode UK Postcode * @return string */ function getUKPostcodeFirstPart($postcode) { // validate input parameters $postcode = strtoupper($postcode); // UK mainland / Channel Islands (simplified version, since we do not require to validate it) if (preg_match('/^[A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?)\s*?\d[A-Z][A-Z]$/i', $postcode)) return preg_replace('/^([A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?))\s*?(\d[A-Z][A-Z])$/i', '$1', $postcode); // British Forces if (preg_match('/^(BFPO)\s*?(\d{1,4})$/i', $postcode)) return preg_replace('/^(BFPO)\s*?(\d{1,4})$/i', '$1', $postcode); // overseas territories if (preg_match('/^(ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)\s*?(1ZZ)$/i', $postcode)) return preg_replace('/^([A-Z]{4})\s*?(1ZZ)$/i', '$1', $postcode); // well ... even other form of postcode... return it as is return $postcode; }

Test data:

echo 'TW135BQ -> ', getUKPostcodeFirstPart('TW135BQ'), "\n"; echo 'gir0aa -> ', getUKPostcodeFirstPart('gir0aa'), "\n"; echo 'RG5 3SQ -> ', getUKPostcodeFirstPart('RG5 3SQ'), "\n"; echo 'AB51 0GQ -> ', getUKPostcodeFirstPart('AB51 0GQ'), "\n"; echo 'YO104EA -> ', getUKPostcodeFirstPart('YO104EA'), "\n"; echo 'SE154NS -> ', getUKPostcodeFirstPart('SE154NS'), "\n"; echo 'W1B4BD -> ', getUKPostcodeFirstPart('W1B4BD'), "\n";

Results:

TW135BQ -> TW13 gir0aa -> GIR0AA RG5 3SQ -> RG5 AB51 0GQ -> AB51 YO104EA -> YO10 SE154NS -> SE15 W1B4BD -> W1B

Please note: It is your responsibility to provide valid postcode.

EDIT: oops, it does not cover GIR 0AA, sorry

更多推荐

php获取邮政编码的第一部分

本文发布于:2023-10-31 06:53:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一部分   邮政编码   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!