tags:

views:

26

answers:

3

It says that it "expects parameter 1 to be string". So i guess you cant have an array in preg_match. But what should I use instead? Thanks

$config['forbidden_names'] = array('admin', 'moderator', 'hoster');


if (preg_match($config['forbidden_names'], $string)) echo "Forbidden name!"; 

edit: in_array maybe. lol. im stupid

+1  A: 
if (in_array($string, $config['forbidden_names']))
    echo "Forbidden name!"; 

Or if you must use regex:

if (preg_match('/^(?:admin|moderator|hoster)$/', $string))
    echo "Forbidden name!"; 
Artefacto
Your regular expression does match strings that either start with `admin`, have `moderator` at any position, or end with `hoster`.
Gumbo
@Gumbo Eh damn precedence. Fixed, but feel free to edit any time.
Artefacto
A: 

try this

if (preg_match('/(admin|moderator|hoster)/i', $string)) echo "Forbidden name!";

btw i would rather add to your roles model/table a new column/settings var that manages stuff user is allowed to add. this looks insecure to me.

antpaw
It's exactly what we don't want to do. This array is just an example.
Paulocoghi
a very bad example then, bacause i can imagen a form where user could edit his roles and by only editing the dom he could assign himself a admin role. so not sure what he ment
antpaw
+1  A: 

Change your second line to:

if (in_array($string, $config['forbidden_names'])) echo "Forbidden name!";

preg_match uses a regular expression, you should stay away from it unless you really need to search through a regex string, because it is slow.

Duane13