Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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