onedumbcoder Posted July 31, 2009 Share Posted July 31, 2009 is it possible to only all white spaces in the middle and not at the start or end of a value using preg_match? Quote Link to comment https://forums.phpfreaks.com/topic/168233-preg-match-allow-spaces-in-only-the-middle-not-at-the-start-or-at-the-end/ Share on other sites More sharing options...
.josh Posted July 31, 2009 Share Posted July 31, 2009 ~^\S.*\S$~ however... what is your goal for this? Are you trying to kick back an error for invalid input or something? If the overall goal of this is to receive the data w/out the spaces on the ends, don't make the user re-enter it; just trim it. Not everything has to kick back an error. This is something unnecessary, like how when forms are programmed to kick back errors for improperly formatted phone numbers or dates. There's no reason to make the user go through the effort to enter in something exactly right, like dashes instead of parenthesis or slashes, etc.. Quote Link to comment https://forums.phpfreaks.com/topic/168233-preg-match-allow-spaces-in-only-the-middle-not-at-the-start-or-at-the-end/#findComment-887362 Share on other sites More sharing options...
onedumbcoder Posted July 31, 2009 Author Share Posted July 31, 2009 I was trimming it with trim but I doubled guess myself and thought maybe I should kick back an error. Quote Link to comment https://forums.phpfreaks.com/topic/168233-preg-match-allow-spaces-in-only-the-middle-not-at-the-start-or-at-the-end/#findComment-887372 Share on other sites More sharing options...
onedumbcoder Posted July 31, 2009 Author Share Posted July 31, 2009 now I have another issue, I am trying to allow numbers, letters and spaces but it is not working. here is what i have: /^[0-9a-zA-Z](\s)+$/ Quote Link to comment https://forums.phpfreaks.com/topic/168233-preg-match-allow-spaces-in-only-the-middle-not-at-the-start-or-at-the-end/#findComment-887379 Share on other sites More sharing options...
.josh Posted July 31, 2009 Share Posted July 31, 2009 okay so that regex tells it to match if there is 1 letter or number at the beginning of the string, followed by one or more spaces, until end of string. ~^[a-z0-9\s]+$~i This is what you need. The regex itself will allow for spaces on the edges, but if you trim it either before or after, the rest of the regex will match what you want. Quote Link to comment https://forums.phpfreaks.com/topic/168233-preg-match-allow-spaces-in-only-the-middle-not-at-the-start-or-at-the-end/#findComment-887381 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.