pianoman993 Posted February 17, 2009 Share Posted February 17, 2009 Hello there PHP experts. I have a small expression that checks to make sure the given string has Only alpha characters, digits, underline or dash characters. However, would it be possible to edit it to allow one space between words? Here is what I've got: '/^([a-z0-9])([a-z0-9_\-])*$/ix' Example "Example String" (the expression allows one space between example and string) An even more adventurous example: "Another Example String" Still allows a space between words If anyone has any ideas please post! Thank you for your time! - Pianoman993 Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/ Share on other sites More sharing options...
trq Posted February 17, 2009 Share Posted February 17, 2009 However, would it be possible to edit it to allow one space between words? You might want to post your current expression. Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/#findComment-764690 Share on other sites More sharing options...
pianoman993 Posted February 17, 2009 Author Share Posted February 17, 2009 I figured you'd say that as soon as I posted it. *Edited* Thanks for the speedy response Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/#findComment-764693 Share on other sites More sharing options...
nrg_alpha Posted February 18, 2009 Share Posted February 18, 2009 You mean something like this? $str = 'Another Example String 1_2-3'; echo $str . "<br />\n"; if (preg_match('#^(?:[\w-]+ ?)+$#', $str)){ echo 'Legal format!'; } else { echo 'Illegal format!'; } Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/#findComment-764703 Share on other sites More sharing options...
pianoman993 Posted February 18, 2009 Author Share Posted February 18, 2009 I think that did that trick! And that does everything the regular expression accomplished plus spacing correct? Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/#findComment-764706 Share on other sites More sharing options...
nrg_alpha Posted February 18, 2009 Share Posted February 18, 2009 It takes spacing into account. Have you not tried it out? Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/#findComment-764721 Share on other sites More sharing options...
pianoman993 Posted February 18, 2009 Author Share Posted February 18, 2009 Yup, works like a charm. You're a genius! Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/#findComment-764727 Share on other sites More sharing options...
nrg_alpha Posted February 18, 2009 Share Posted February 18, 2009 The pattern could also be: '#^(?:[\w-]+ ?)*$#' Note the * instead of the + after the closing non-capturing bracket. Either version seems to work though. Quote Link to comment https://forums.phpfreaks.com/topic/145660-solved-allow-one-space-between-words/#findComment-764730 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.