-1

Well guys, I'm here because I want to do something advanced on my site, and I don't know how do this and if is possible do this.

It's like this:

Someone will register on my site and have an function to verify if this name is blocked or not in "array()", but some specials characters is avaible to put on your name, like ".", "-"... And if I register like this name "A.D.M", my verification doesn't work.

You guys can help me with this?

My function:

final static function isBlockedName($name) {
    $blockedNames = array ('mod', 'adm', 'staff');
    foreach($blockedNames as $list) {
        if(strtolower($name) == strtolower($list)) {
            self::AddBan('user','157788000',self::getUserID($name),'Nome Impróprio','3');
            session_destroy();

            header('Location: /');
            exit;
        }
    }
    foreach($blockedNames as $two) {
        if(strpos(strtolower($name), strtolower($two)) !== false) {
            self::AddBan('user','157788000',self::getUserID($name),'Nome Impróprio','3');
            session_destroy();

            header('Location: /');
            exit;
        }
    }
    return false;
}

Sorry for my bad english :(

jh314
  • 27,144
  • 16
  • 62
  • 82
  • `strpos` will return `0` on an exact match, so you don't need the first loop, it overlaps with the second since you correctly check with `!== false`. – Halcyon Apr 10 '15 at 13:54

1 Answers1

0

You can use str_replace to remove those special characters from the name in the comparison. Change

if(strtolower($name) == strtolower($list)) {

to

if(strtolower(str_replace(array('.', '-'), '', $name)) == strtolower($list)) {

An alternative way is to use in_array instead of the foreach loop:

if(in_array(str_replace(array('.', '-'), '', $name), $blockedNames)) {
    // do something
}
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94