powelly Posted May 4, 2007 Share Posted May 4, 2007 I need to trim a postcode held in a string to the first space. ie m1 4bu becomes m1 ol8 9ez becomes ol8 ec3 9dz becomes ec3 I know i could use subsrt if it was always the same number of characters but this can vary so im unsure. I looked at trim but that seems to be for whitespaces at the front or back. Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/ Share on other sites More sharing options...
taith Posted May 4, 2007 Share Posted May 4, 2007 not tested... but it should grab the first space, and cut all after it... $key='m1 4bu '; $shrunk=substr($key,0,strpos($key,' ')); Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245237 Share on other sites More sharing options...
jitesh Posted May 4, 2007 Share Posted May 4, 2007 $key='m1 4bu '; $shrunk=explode(" ",$key); echo $shrunk[0]; Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245238 Share on other sites More sharing options...
Barand Posted May 4, 2007 Share Posted May 4, 2007 list($post_code_area) = explode(' ', $postcode); Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245241 Share on other sites More sharing options...
powelly Posted May 4, 2007 Author Share Posted May 4, 2007 list($post_code_area) = explode(' ', $postcode); Thanks that worked, However its now introduced a new problem, what if the user does not enter a space in the postcode! Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245254 Share on other sites More sharing options...
taith Posted May 4, 2007 Share Posted May 4, 2007 then theres no way of knowing wether the 1st section is 2 or 3 characters long... Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245258 Share on other sites More sharing options...
Barand Posted May 4, 2007 Share Posted May 4, 2007 Is the last section always 3 chars, or do some postcodes have format something like L3 11BU? Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245262 Share on other sites More sharing options...
powelly Posted May 4, 2007 Author Share Posted May 4, 2007 Is the last section always 3 chars, or do some postcodes have format something like L3 11BU? not sure Im looking at this http://javascript.internet.com/forms/uk-postcode-validation.html at the moment with the view to validating the postcode before submitting it so that way I only have to trim valid postcodes Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245264 Share on other sites More sharing options...
powelly Posted May 4, 2007 Author Share Posted May 4, 2007 just spotted this "The inward code, the part to the right of the gap, must always be 3 characters" so if there is no space I can lose the last 3 characters! Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245265 Share on other sites More sharing options...
ToonMariner Posted May 4, 2007 Share Posted May 4, 2007 All post codes end in NUMBER LETTER LETTER so all you need is to strip the last 3 characters and then trim any white space.. <?php $postcode = 'yourstring'; $temp = trim($postcode); $code = substr(($temp, 0 ,strlen($temp) - 3); $code = trim($code); ?> Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245267 Share on other sites More sharing options...
MadTechie Posted May 4, 2007 Share Posted May 4, 2007 what about <?php $subject = "m1 4bu becomes m1 ol8 9ez becomes ol8 ec3 9dz becomes ec3"; $result = preg_replace('#(\w{1,2}\d{1})\s\d{1,2}\w{1,2}#i', '$1', $subject); echo $result; ?> EDIT: corrected Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245270 Share on other sites More sharing options...
ToonMariner Posted May 4, 2007 Share Posted May 4, 2007 or <?php $postcode = 'yourstring'; $result = preg_replace('/(.*)?([0-9][a-zA-Z]{2})$/', $1,trim($postcode)) ?> Link to comment https://forums.phpfreaks.com/topic/49957-trim-postcode-like-m1-4bu-to-m1/#findComment-245273 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.