0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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!"; 

2 Comments

Your regular expression does match strings that either start with admin, have moderator at any position, or end with hoster.
@Gumbo Eh damn precedence. Fixed, but feel free to edit any time.
0

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.

2 Comments

It's exactly what we don't want to do. This array is just an example.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.