lukaswhite/uk-postcode
最新稳定版本:1.0
Composer 安装命令:
composer require lukaswhite/uk-postcode
包简介
A PHP class that represents a UK Postcode; for validating, formatting and querying
README 文档
README
A PHP class that represents a UK Postcode.
It allows you to:
- validate a postcode
- format a postcode correctly
- get the outcode, inward code or postcode sector
For example, suppose you request users provide their postcode when filling out their address; there's a very strong possibility they'll enter something like sw1a2aa. This class not only allows you to check that it's a valid UK postcode, but also format it correctly — in this example, that means SW1A 2AA.
Installation
Via Composer:
composer require lukaswhite\uk-postcode
Create an instance
use Lukaswhite\UkPostcode\UkPostcode; $postcode = new UkPostcode('sw1a2aa');
Validate a Postcode
if ($postcode->isValid()) { // do something... }
Alternatively, use the static method:
if (UkPostcode::validate('sw1a2aa')) { // do something... }
Format a Postcode
$postcode = new UkPostcode('sw1a2aa'); print $postcode->formatted(); // outputs "SW1A 2AA"
Outcodes
The outcode is the first part of a UK postcode. To illustrate:
$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"
Inward Codes
The inward code is the bit after the outcode. To illustrate:
$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"
Sectors
The sector is the outcode, followed by the first digit of the inward code. To illustrate:
$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"
Miscellany
- The class implements the magic
__toString()method, which will return a formatted version of the postcode.
统计信息
- 总下载量: 11.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-10-18