mackin Posted December 12, 2011 Share Posted December 12, 2011 I have a table with postcodes in it. At some point some of the codes will be entered with spaces (LA12 5TH) and some without (LA17HU) what i need is a function to check if there is a space, if not - count back 3 characters from the end of the string and insert a space. I don't know which string function to use - looked at pad but that seems to be for either end of string and not the middle - any ideas? Link to comment https://forums.phpfreaks.com/topic/253031-insert-a-space-into-a-string-if-there-isnt-a-space-already/ Share on other sites More sharing options...
blacknight Posted December 12, 2011 Share Posted December 12, 2011 use wordwrap($str, 3, " ", true) $str being your postalcode 3 being every 3 characters im canadian so this is our standard but it can be changed to any number and it does not leave a traling space ither Link to comment https://forums.phpfreaks.com/topic/253031-insert-a-space-into-a-string-if-there-isnt-a-space-already/#findComment-1297269 Share on other sites More sharing options...
mackin Posted December 12, 2011 Author Share Posted December 12, 2011 thx for the reply blackknight can i use a negative number - the count has to start from the end of string. The Part before the space can be 3 or 4 chars, but always has 3 after the space.?? Link to comment https://forums.phpfreaks.com/topic/253031-insert-a-space-into-a-string-if-there-isnt-a-space-already/#findComment-1297277 Share on other sites More sharing options...
kicken Posted December 12, 2011 Share Posted December 12, 2011 if (strpos($str, ' ')===false){ //no space $str = substr_replace($str, ' ', -3, 0); //insert space } Link to comment https://forums.phpfreaks.com/topic/253031-insert-a-space-into-a-string-if-there-isnt-a-space-already/#findComment-1297282 Share on other sites More sharing options...
mackin Posted December 12, 2011 Author Share Posted December 12, 2011 looks good kicken - ill try that in the AM, Cheers Link to comment https://forums.phpfreaks.com/topic/253031-insert-a-space-into-a-string-if-there-isnt-a-space-already/#findComment-1297283 Share on other sites More sharing options...
mackin Posted December 13, 2011 Author Share Posted December 13, 2011 works a treat Kicken thx v much Link to comment https://forums.phpfreaks.com/topic/253031-insert-a-space-into-a-string-if-there-isnt-a-space-already/#findComment-1297476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.