PHP Find the first part of a zip code in the UK when a full or part can be entered

Someone, please, relieve me of my suffering and help me solve this problem.

I have a zip code search box that allows people to enter full zip codes (e.g. BS34 5GF) or parts zip codes (e.g. BS34).

My search in the zip code requires only the first part of the zip code, and I am trying to find the most efficient way to trim the string so that it has only the first section without explicitly specifying the format into which it is entered.

Here are some examples of codes: B2 5GG, B22 5GG, BS22 5GG, B25GG, BS25GG, BS225GG, BI 2, BS2, BS22

This shows how many options can be. What is the best way to ensure that I always get the first part of the zip code?

+4
source share
4 answers

IMHO modes are the correct solution to the problem.

Ignoring the BFPO address now, try:

if (preg_match("(([AZ]{1,2}[0-9]{1,2})($|[ 0-9]))", trim($postcode), $match)) { $region=$match[1]; } 
+3
source

If you use regular expressions to match British postal codes (part or whole), you are doing it wrong. Also, note that before you go any further, no matter how you write your code, there is one case where the format will be ambiguous: BS22 may well belong to BS2 2AB or BS22 5GS. There is absolutely no way to tell, and you will need to make a decision based on this.

The proposed algorithm considers the case of BS22 as BS22. It looks like this:

 <?php function testPostcode($mypostcode) { if (($posOfSpace = stripos($mypostcode," ")) !== false) return substr($mypostcode,0,$posOfSpace); // Deal with the format BS000 if (strlen($mypostcode) < 5) return $mypostcode; $shortened = substr($mypostcode,0,5); if ((string)(int)substr($shortened,4,1) === (string)substr($shortened,4,1)) { // BS000. Strip one and return return substr($shortened,0,4); } else { if ((string)(int)substr($shortened,3,1) === (string)substr($shortened,3,1)) { return substr($shortened,0,3); } else return substr($shortened,0,2); } } // Test cases $postcodes = array("BS3 3PL", "BS28BS","BS34","BS345","BS32EQ"); foreach ($postcodes as $k => $v) { echo "<p>".$v." => ".testPostcode($v)."</p>"; } 

It is faster and easier to maintain than regular expression.

+2
source

How about if you pulled out the spaces and checked the length. I think all zip codes should be at least 5 characters long.

If the zip code is less than 5 characters, take it all as the area code. If it is more than 5 characters, delete the last 3 characters and take the remainder as the area code:

 function getPostCodeArea($pcode){ $pcode = str_replace(' ', '', $pcode); if(strlen($pcode) > 4){ if(is_numeric($pcode{strlen($pcode)-1})){ $pcode = substr($pcode, 0, 4); }else{ $pcode = substr($pcode, 0, strlen($pcode)-3); } return $pcode; }else{ return $pcode; } } 
+1
source

This will complete the task:

Note: this is a simplified regular expression for a zip code - for a more complete check more fully

 function getOutwardPostcodePart($postcode) { $matches = array(); if (preg_match("/^([a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1}) {0,1}([0-9][A-Za-z]{2}){0,1}$/", $postcode, $matches )) { return $matches[1]; } return false; } 

I don’t think that one way or another will have to handle the unlikely situation where the actual external part of the zip code is entered with only a partial internal part.

+1
source

Source: https://habr.com/ru/post/1478985/


All Articles