LanceT Posted January 10, 2008 Share Posted January 10, 2008 What would the regex be for Only allowing numbers, letters, dashes, and underscores Thanks. Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 10, 2008 Share Posted January 10, 2008 do some searching, everyone asks this question http://www.phpfreaks.com/tutorials/52/0.php character classes if (preg_match('~^[-_a-z0-9]+$~im', '23478sdf935_')) { echo 'passes'; } better yet match what you don't want: if (!preg_match('~[^-_a-z0-9]+~i', '23478sdf935_')) { //at least 1 char with +, null str ok? use * then echo 'passes'; } Quote Link to comment Share on other sites More sharing options...
LanceT Posted January 12, 2008 Author Share Posted January 12, 2008 What part of the expression allows the underscores and dash? Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 12, 2008 Share Posted January 12, 2008 Insert Quote What part of the expression allows the underscores and dash? ~^[-_a-z0-9]+$~im [ ] denotes a character class search google to learn about them a-z, 0-9 are ranges all other characters are taken as string literals as you can see - and _ are the first two string literals in the char class however in this pattern: ~[^-_a-z0-9]+~i no part of it allows dashes or underscores the ^ symbol matches everything that is not what's in the char class Quote Link to comment Share on other sites More sharing options...
effigy Posted January 14, 2008 Share Posted January 14, 2008 ~^[-_a-z0-9]+$~im Why are you using the /m modifier? Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 14, 2008 Share Posted January 14, 2008 because the m modifier turns on the meanings for the ^ and $ characters to mean anchors of the start and beginning of a line Quote Link to comment Share on other sites More sharing options...
effigy Posted January 14, 2008 Share Posted January 14, 2008 From the docs: By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect. In this case, using /m could yield unwanted results for the poster. For example, "a\n-+$%^&#@" would be valid. 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.