PHP code example of lukaswhite / uk-postcode

1. Go to this page and download the library: Download lukaswhite/uk-postcode library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lukaswhite / uk-postcode example snippets


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"