Kryptix Posted December 26, 2011 Share Posted December 26, 2011 I'm using this to check username's are valid (letters, numbers and spaces): if (!preg_match("/^[a-zA-Z0-9 ]+$/", $username)) However, people can use names like "Hello 4" (5 spaces in a row). How would I eliminate this? Is it best to just replace the 5 spaces with 1 or would that confuse users? Is it best to just throw an error up? Can someone change the regex above to fix it or is it more complicated than that? Quote Link to comment https://forums.phpfreaks.com/topic/253834-no-more-than-1-space-next-to-each-other/ Share on other sites More sharing options...
ktroztafy Posted December 26, 2011 Share Posted December 26, 2011 Try : preg_replace('/( )+/', ' ', $username); to remove all white-space, then use your own code to check the $username Quote Link to comment https://forums.phpfreaks.com/topic/253834-no-more-than-1-space-next-to-each-other/#findComment-1301339 Share on other sites More sharing options...
jcbones Posted December 26, 2011 Share Posted December 26, 2011 So you want to allow 1 space, but no more? The above code given will remove ALL spaces. 1 space but no more is: preg_replace('~\s\s+~','',$username); Quote Link to comment https://forums.phpfreaks.com/topic/253834-no-more-than-1-space-next-to-each-other/#findComment-1301386 Share on other sites More sharing options...
melloorr Posted December 26, 2011 Share Posted December 26, 2011 trim($username); Quote Link to comment https://forums.phpfreaks.com/topic/253834-no-more-than-1-space-next-to-each-other/#findComment-1301387 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2011 Share Posted December 26, 2011 trim() only removes leading and trailing whitespace, not whitespace between other characters. Quote Link to comment https://forums.phpfreaks.com/topic/253834-no-more-than-1-space-next-to-each-other/#findComment-1301389 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.