0

I found this useful function here on SO, but I need it to work slightly differently. I have to take JUST the first two characters of the postcode, without any of the numbers so that they can be used to compare against a table that hold postcode prefixes and regions. Searching this database with CH7 would not return any results as the database contains only CH.

function getUKPostcodeFirstPart($postcode)
{
// validate input parameters
$postcode = strtoupper($postcode);

// UK mainland / Channel Islands (simplified version, since we do not require to validate it)
if (preg_match('/^[A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?)\s*?\d[A-Z][A-Z]$/i', $postcode))
return preg_replace('/^([A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?))\s*?(\d[A-Z][A-Z])$/i', '$1', $postcode);
// British Forces
if (preg_match('/^(BFPO)\s*?(\d{1,4})$/i', $postcode))
    return preg_replace('/^(BFPO)\s*?(\d{1,4})$/i', '$1', $postcode);
// overseas territories
if (preg_match('/^(ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)\s*?(1ZZ)$/i', $postcode))
    return preg_replace('/^([A-Z]{4})\s*?(1ZZ)$/i', '$1', $postcode);

// well ... even other form of postcode... return it as is
return $postcode;
}

If I test this using the following

$postcode ="CH7 3DT"; 
echo 'CH7 3DT -> ', getUKPostcodeFirstPart($postcode), "\n";

I get a result of CH7, which exactly what the code is supposed to do. But I need to have a return of just CH

B31 5SN should return just B, SY4 4RG should return just SY

I've tried removing various parts of the preg_replace to reduce the output but I either get nothing, or the full postcode again. To be honest, preg_replace is so confusing to me!

Steve Price
  • 600
  • 10
  • 28
  • 1
    do you want to get only [a-zA-Z] but not numbers? – Akam May 20 '16 at 23:50
  • 1
    **"I get a result of CH7, which exactly what the code is supposed to do. But I need to have a return of just CH**" Does it make sense to you ? – Pedro Lobito May 20 '16 at 23:50
  • Possible duplicate of [UK Postcode Regex (Comprehensive)](http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive) – Pedro Lobito May 20 '16 at 23:52
  • @Akam Yes, I want the letters but not the numbers. But only the letters before the numbers, not all of them. – Steve Price May 20 '16 at 23:52
  • @PedroLobito It isn't a duplicate of another question. I specfically need ONLY the first one or two letters of the postcode which are then matched to the data stored in a mysql table to give regions. For instance, if you queried the table with FK, it would return Scotland. This is specifically what i want the first one or two letters for as searching the database with the number as well would return zero results. – Steve Price May 20 '16 at 23:58
  • 2
    `preg_match('/^([a-z]+)(\d+)/i', $postcode, $out);` this will match only letters at first then `echo $out[1]` contain your desired result. see https://3v4l.org/QWEEQ – Akam May 21 '16 at 00:00
  • `preg_match('/(^[A-Z*]+)/gim', $postcode, $out);` – Farkie May 21 '16 at 00:04

1 Answers1

1
preg_replace("/[^A-Za-z]/", "", getUKPostcodeFirstPart($postcode));

This should replace any non-alphabet characters with nothing, essentially stripping them from the string.

citrusy
  • 161
  • 8