AdRock Posted March 19, 2008 Share Posted March 19, 2008 I have a string and I want to split it into 2 parts but the last part has to be 3 characters. What it is, is a uk postcode which is in the database like CF127HG and i want to format the output like CF12 7HG How do i do that? Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 19, 2008 Share Posted March 19, 2008 substr() could do it: $first_part = substr($postcode, 0, strlen($postcode)-3); $second_part = substr($postcode, strlen($postcode)-4); Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 19, 2008 Author Share Posted March 19, 2008 Thinking about it, the word doesn't have to be split but a space added to the string before the last 3 characters Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 19, 2008 Share Posted March 19, 2008 You'd still use the substr()'s but concatenate them together with a space in between: $postcode = substr($postcode, 0, strlen($postcode)-3) . ' ' . substr($postcode, strlen($postcode)-4); Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 19, 2008 Author Share Posted March 19, 2008 thanks jeremy, worked a treat. just had to change them both to -3 to allow for smaller postcodes Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.