RobertP Posted November 11, 2011 Share Posted November 11, 2011 so i am not a huge fan of these snippets, but i need some help creating one. string requirements: -minimum length of 3. -maximum length of 28. -can only contain letters (both capital and none) and numbers. -string can contain - (normal dash), they can not be at the very beginning/end of the string. --> also, the dash can not be concurrent, string can not be 'asd--asd', but only 'asd-asd' or 'as-d-as-d' this is what i have, i only have an issue with the dash. function isValidDisplayName($displayName){ return preg_match('/[a-zA-Z0-9-]{3,28}/',$displayName); } PS: don't just post the code, please explain so i can learn Quote Link to comment Share on other sites More sharing options...
xyph Posted November 11, 2011 Share Posted November 11, 2011 Hope this helps. Start of the string, followed by 1 allowed character that's not a dash Followed by 1-26 characters that are either a letter number, or a dash not followed by another dash Followed by 1 allowed character that's not a dash Followed by the end of the string '% ^ # Match the start of the string [a-z0-9] # Match a letter or number once (?: # Start a non-capturing group [a-z0-9] | -(?!-) # Match either a letter/number or a dash not followed by another dash (negetive lookahead) ){1,26} # End the non capturing group, and make sure it matches between 1 and 26 times [a-z0-9] # Match a letter or number once $ # Match the end of the string %ix' i - case insensitive, x - free spacing 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.