0

I'm trying to pick out post codes from an array. I am using the post office's regex to find the matches.

$postcodeRegex = "/(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})/";

foreach( $content as $key => $line ){
    if( preg_match($postcodeRegex, $line, $matches) !== false ) {
        $points[] = $key;
    }
}

But preg_match keeps is producing false positives. For example the line below is shown as a match.

HYPERLINK "mailto:email@address.com" email@address.com

My regex skills are very poor. How do i cut down on these false positives?

thanks

edit, a sample of elements in the array:

[2] => Firstname lastname
[6] => 99 Example Street, Example Town, Example City, EX4 3PL
[8] => HYPERLINK "mailto:email@address.com" email@address.com
[10] => 07712 345678
[16] => KEY SKILLS
[18] => Technical Skills
[20] => Microsoft Outlook
[22] => Microsoft Word
[24] => Microsoft Excel

1 Answers1

0

It seems that you're using the regex supplied by the UK Government

But first check if you're dealing with a valid UK post code:

$accepted_numbers = array_merge(range(15, 22), range(31, 41));

if (preg_match('#^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$#', $postcode) && substr($postcode, 0, 2) == 'DN' && in_array(substr($postcode, 2, 2), $accepted_numbers)) {
       // The UK post code is valid according to your criteria
}


Update based on your comments:

 if (preg_match('/(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})/', $address )) {

preg_match('/(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})/im', $address , $postcode);

echo $postcode[0];

 }

Output:

EX4 3PL

DEMO

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268