pugboy Posted November 21, 2008 Share Posted November 21, 2008 I have tried looking at these expressions, and I really don't know how anyone can understand them. Amazing that people can I need to allow only A-Z (upper or lowercase) 0-9 and spaces, dashes, and underscores... I will be replacing the spaces with dashes however, so that this string is URL friendly and more pleasing to the eye than 'index.php?id=1384'... EDIT: IE is behaving oddly... SUbmitted on accident and I can barely edit... Can anyone point me to a premade expression or help me find a good beginner tutorial on these? Quote Link to comment Share on other sites More sharing options...
corbin Posted November 21, 2008 Share Posted November 21, 2008 Google "regular expressions tutorial" or there's a sticky around here some where. Anyway: if(preg_match('/^[\dA-Za-z _-]+$/', $str)) { //valid } That would be one or more of them. If you want any amount, change the + to a *. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 Or, to shorten it up a bit: <?php if (preg_match('/^[\w-]+$/', $str) { //good } else { //not so good } Quote Link to comment Share on other sites More sharing options...
corbin Posted November 21, 2008 Share Posted November 21, 2008 Doesn't \w match all kinds of whitespace? Like tabs? (And new lines in multiline mode) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 Doesn't \w match all kinds of whitespace? Like tabs? (And new lines in multiline mode) That's \s. \w is "word characters", or [A-Za-z0-9_]. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 21, 2008 Share Posted November 21, 2008 Ahhhhhhhh..... That's why I don't use character classes. Screw memorization, although \s isn't hard to remember since space. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 Ahhhhhhhh..... That's why I don't use character classes. Screw memorization, although \s isn't hard to remember since space. \d, digit \w, word \s, space =P I think they tried to make it pretty easy to remember. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 21, 2008 Share Posted November 21, 2008 Yeah, I usually use \d just because it's easier than 0-9. I usually remember \s too. Not sure why I was thinking the word class included space. Quote Link to comment 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.