Split PHP Variable Value - Help

Hi Guys,

Right guys i need some help and I’ve been thinking about this for some time and cant come up with a way round the issue. I have a form which consumers fill in, they need to enter their UK Post Code. Now i need to send that info onto our client but they require the postcode be sent in 2 strings. For example:




Now there’s different formats a UK Post Code can be:

Format Example Coverage
A9 9AA M1 1AA B, E, G, L, M, N, S, W postcode areas
A99 9AA B33 8TH
AA9 9AA CR2 6XH All postcode areas except B, E, G, L, M, N, S, W, WC
AA99 9AA DN55 1PT
A9A 9AA W1A 1HQ E1W, N1C, N1P, W1 postcode districts (high density areas where codes ran out)
AA9A 9AA EC1A 1BB WC postcode area; EC1–EC4, NW1W, SE1P, SW1 postcode districts (high density areas where codes ran out)

As you can see the different formats, now the issue i have is how do i go about working out where I should split the postcode value? The obvious thing for me to do would be to ask people to enter their first part and second part of their postcode in 2 different fields but I don’t really want to do it that way as it may confuse consumers.

Can anyone help me out please?

Thank you…

Why not have 2 input fields next to each other?

As that will confuse the consumer if theres 2 fields saying Postcode1 and Postcode2 which means conversion rates will be low.

The last 3 characters are always the final part of the post code.

Lop them off, and what remains should be the first part – does that pass all tests?


So you might be sent a valid looking string – but you (and your end client) might be more interested in trying to validate that you have been sent a real postcode before saving:

Try using one of these [google]postcode api uk[/google]

Haha glad someone spotted this! Thank you…!

How do i go about splitting the value of postcode into postcode1 and postcode2 with postcode2 having the last 3 values and postcode1 having the remaining?

I know how to do it if theres ---- in between them as id just use this:

list($postcode1, $postcode2) = explode("----", $postcode);

Any help would be great.


start with [fphp]substr/fphp

Specifically, note that the start parameter of substr can carry a negative number.

Hi Guys,

Thanks for the help, sorry for not responding sooner i never got an emailing there was a new reply. I’ve managed to code what i need it do to but i have an issue, below is my code:


function countchar ($string) { 
$resultpostcode = strlen ($string)  -   substr_count($string, ' '); 
echo $resultpostcode;  

countchar ($postcode);

if ( $resultpostcode == 6 ) {
		echo "Postcode1 ";
		echo substr($postcode, 0, 3);
		echo "<br>";
		echo "Postcode2 ";
		echo substr($postcode, 3);

} elseif( $resultpostcode == 7 ){

		echo "Postcode1 ";
		echo substr($postcode, 0, 4);
		echo "<br>";
		echo "Postcode2 ";
		echo substr($postcode, 4);

}else {
	echo "error";

If the value of postcoderesult is 6 (it even outputs 6) then it wont echo what it should be doing, the same if its 7. It just keeps echoing Error.

Any help would be great please.

Thank you.

Bump. Any help would be great.


Lets say for now that the test values which should all pass are held in this array:

$a= array(
"WA2 8PR",
"WA2 8PR ",
"WA2-8PR ",
"(wa2 8PR", 

…and the result must each equal array(“WA2”, “8PR”);

You need to create a function which

a) removes all of the spaces
b) removes anything which is not a permitted letter or a permitted number
c) uppercases what is left
d) checks the length is valid
// else return false
e) takes of the last 3 chars
f) isolates the remaining chars
g) returns an array with the previous 2 values

Why don’t you give it your best shot at running some code against that test array and see how close you can get, then ask for help if you get stuck.

here’s a start at a) we dont validate vs the final array for the time being, just against the expected result: “WA28PR”

function checkPostcode($pc){

$pc = str_replace(" ", "", $pc);

return $pc;

foreach($a as $pcode){
$new = checkPostcode($pcode);

if( $new === "WA28PR"){
echo "PASS ! ";
echo checkPostcode($pcode) , PHP_EOL;

// gives

So, what’s next what do you add to the function to get more passes?
What can you add to the original array to make your tests more lifelike?
You could create a new array of values which should always fail (return false).

That is one way of thinking about solving your own problems.


Step 1 is actually defining your problem.
Postcodes arnt simply 6 character strings, unfortunately.
For Cups’ sake, i’m going to define the postcode in a Regex pattern. This will help him narrow down what you need to do.


(Hint: This regex (slightly altered) could be used to simplify your problem greatly…)