lukaswhite / uk-postcode
此包最新版本(1.0)没有可用的许可信息。
一个表示英国邮政编码的PHP类;用于验证、格式化和查询
1.0
2018-10-18 10:17 UTC
Requires
- php: >=7.0
Requires (Dev)
- phpunit/php-code-coverage: ^6.0
- phpunit/phpunit: 7.0
This package is auto-updated.
Last update: 2024-09-18 23:42:59 UTC
README
一个表示英国邮政编码的PHP类。
它允许您
- 验证邮政编码
- 正确格式化邮政编码
- 获取区号、内向码或邮政编码区段
例如,假设您要求用户在填写地址时提供他们的邮政编码;他们很可能输入类似 sw1a2aa
的内容。这个类不仅可以检查它是否是一个有效的英国邮政编码,还可以正确地格式化它——在这个例子中,这意味着 SW1A 2AA
。
安装
通过Composer
composer require lukaswhite\uk-postcode
创建实例
use Lukaswhite\UkPostcode\UkPostcode; $postcode = new UkPostcode('sw1a2aa');
验证邮政编码
if ($postcode->isValid()) { // do something... }
或者,使用静态方法
if (UkPostcode::validate('sw1a2aa')) { // do something... }
格式化邮政编码
$postcode = new UkPostcode('sw1a2aa'); print $postcode->formatted(); // outputs "SW1A 2AA"
区号
区号是英国邮政编码的第一部分。为了说明
$postcode = new UkPostcode('sw1a2aa'); print $postcode->getOutcode(); // outputs "SW1A" $postcode = new Lukaswhite\UkPostcodes\UkPostcode('GL9 1AH'); print $postcode->getOutcode(); // outputs "GL9" $postcode = new UkPostcode('gl91ah'); print $postcode->getOutcode(); // outputs "GL9"
内向码
内向码是区号之后的部分。为了说明
$postcode = new UkPostcode('sw1a2aa'); print $postcode->getInwardCode(); // outputs "2AA" $postcode = new Lukaswhite\UkPostcodes\UkPostcode('GL9 1AH'); print $postcode->getInwardCode(); // outputs "1AH" $postcode = new UkPostcode('gl91ah'); print $postcode->getInwardCode(); // outputs "1AH"
区段
区段是区号,后跟内向码的第一个数字。为了说明
$postcode = new UkPostcode('sw1a2aa'); print $postcode->getSector(); // outputs "SW1A 2" $postcode = new Lukaswhite\UkPostcodes\UkPostcode('GL9 1AH'); print $postcode->getSector(); // outputs "GL9 1" $postcode = new UkPostcode('gl91ah'); print $postcode->getSector(); // outputs "GL9 1"
杂项
- 该类实现了魔法
__toString()
方法,它将返回邮政编码的格式化版本。