Skip to content

Instantly share code, notes, and snippets.

@mattsandersuk
Last active April 10, 2020 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsandersuk/d40008f533e455d6401a5a488df5b43f to your computer and use it in GitHub Desktop.
Save mattsandersuk/d40008f533e455d6401a5a488df5b43f to your computer and use it in GitHub Desktop.
<?php
/**
* Split a postcode
* @param string $postcode
* @return array containing both outer and inner postcodes
*/
function split_postcode($postcode)
{
// remove spaces to be safe
$postcode = str_replace(' ', '', $postcode);
// get last 3 characters + trim them
$postcodeInner = trim(substr($postcode, -3));
// get remaining characters + trim them to be safe
$postcodeOuter = trim(str_replace($postcodeInner, '', $postcode));
// return the split
return [
'outer' => str_pad($postcodeOuter, 4, " "),
'inner' => str_pad($postcodeInner, 4, " "),
];
}
<?php
/**
* Split a postcode
* @param string $postcode
* @return array containing both outer and inner postcodes
*/
function split_postcode($postcode)
{
// remove spaces to be safe
$postcode = str_replace(' ', '', $postcode);
// get last 3 characters + trim them
$postcodeInner = trim(substr($postcode, -3));
// get remaining characters + trim them to be safe
$postcodeOuter = trim(str_replace($postcodeInner, '', $postcode));
// return the split
return [
'outer' => $postcodeOuter,
'inner' => $postcodeInner,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment