lupacexi Posted August 2, 2017 Share Posted August 2, 2017 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); } Quote Link to comment https://forums.phpfreaks.com/topic/304468-starting-out-with-expressions/ Share on other sites More sharing options...
Jacques1 Posted August 2, 2017 Share Posted August 2, 2017 Checking the string length is the job of strlen(), so forget about that when you write the regex. There are three logical steps here: Write a pattern for an alphanumerical sequence without a dash. That's the prefix. Write a second pattern for an alphanumerical sequence preceded by a dash. Put the two together and repeat the second with the * quantifier. And learn what anchors are. Right now, you're merely doing a substring search. Quote Link to comment https://forums.phpfreaks.com/topic/304468-starting-out-with-expressions/#findComment-1549174 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.