jumpenjuhosaphat Posted January 20, 2007 Share Posted January 20, 2007 Okay, I've seen this done before, but I can't seem to find it in the search engines again.I need to check to see if there is more than one space in a users input, and if there is, I need to remove the extra spaces, leaving only the one. Can someone tell me how to do this please? Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/ Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 php.net/trim will take off spaces in the end. php.net/substr-count will tell you the number of a substr in a string. http://us2.php.net/manual/en/ref.strings.php Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165139 Share on other sites More sharing options...
jumpenjuhosaphat Posted January 20, 2007 Author Share Posted January 20, 2007 Yeah, I don't need to trim it, and I got substr_count, but that doesn't remove the spaces, it just counts them. I could use str_replace, but that would replace all instances of the substring. I want all but one replaced. Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165144 Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 Which one? Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165147 Share on other sites More sharing options...
ShogunWarrior Posted January 20, 2007 Share Posted January 20, 2007 Do you mean like [b]firstname(space)(space)secondname(space)(space)(space)thirdname[/b] would simply become [b]firstname(space)secondname(space)thirdname[/b]? I.E: Should it collapse extra spaces into single spaces? Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165149 Share on other sites More sharing options...
jumpenjuhosaphat Posted January 20, 2007 Author Share Posted January 20, 2007 Yes, exactly. I'm trying to validate UK post codes, and 1 space is required in UK post codes. If the user enters any more than that, an error is returned. Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165150 Share on other sites More sharing options...
Crimpage Posted January 20, 2007 Share Posted January 20, 2007 You can count them with substr_count($haystack, ' ') or you could use a regular expression to do that... I just searched google for "Regular Expression UK Postcode" and a lot of info came up.Cheers,Dave Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165155 Share on other sites More sharing options...
kenrbnsn Posted January 20, 2007 Share Posted January 20, 2007 You can explode the string on a space, use the [url=http://www.php.net/array_filter]array_filter()[/url] function to remove all spaces from the result of the explode and use implode to put it back together again:[code]<?phpfunction not_space($v) { if ($v != ' ') return($v);}$str = 'This string has multiple spaces in it.';$str2 = implode(' ',array_filter(explode(' ',$str),'not_space'));echo '<pre>$str:' . $str . '<br>$str2:' . $str2 . '</pre>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165156 Share on other sites More sharing options...
jumpenjuhosaphat Posted January 21, 2007 Author Share Posted January 21, 2007 Thanks for your help everyone. Here's what I found:[code]preg_replace('/\s\s+/', ' ', $str);[/code]If there is more than one instance of a space in the string, this will remove all of them but one. Link to comment https://forums.phpfreaks.com/topic/35015-removing-all-but-one-space-from-a-string/#findComment-165161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.