I have a pattern with a small list of words that are illegal to use as nicknames set in a pattern variable like this:
$pattern = webmaster|admin|webadmin|sysadmin
Using preg_match, how can I achieve so that nicknames with these words are forbidden, but registering something like “admin2” or “thesysadmin” is allowed?
This is the expression I have so far:
preg_match('/^['.$pattern.']/i','admin');
// Should not be allowed
Note: Using a \b didn’t help much.
What about not using regex at all ?
And working with
explodeandin_array?For instance, this would do :
It explodes your pattern into an array, using | as separator.
And this :
will get you
Whereas this (same code ; only the word changes) :
will get you
This way, no need to worry about finding the right regex, to match full-words : it’ll just match exact words 😉
Edit : one problem might be that the comparison will be case-sensitive 🙁
Working with everything in lowercase will help with that :
Will get you :
(I saw the ‘
i‘ flag in the regex only after posting my answer ; so, had to edit it)Edit 2 : and, if you really want to do it with a regex, you need to know that :
^marks the beginning of the string$marks the end of the stringSo, something like this should do :
Parentheses are probably not necessary, but I like using them, to isolate what I wanted.
And, you’ll get the same kind of output :
You probably don’t want to use
[and]: they mean “any character that is between us”, and not “the whole string that is between us”.And, as the reference : manual of the preg syntax 😉