简体   繁体   中英

Why does strpos not work here?

Im making a function that checks if a certain username in the process of registration contains certain characters that I dont want the users to have in their username. And it echoes the user what characters they can use.

public function checkUsername($username){       
    if(!empty($username)){  
        $exceptions= "!@~`#$%^&*()+=/\,;:[]{  }|?,<>"
this is line 26 from the error ->if(strpos($username, $exceptions) !=== false ){
        //here it means that something was found
            echo "Cannot contain special characters except - and _ .";
            return false;
        }//since it returns false, nothing after this gets executed.
    if(strlen($username) < 6){
        echo "Username must be at least 6 characters long to register.";
        return false;
    }
    $stmt = $this->db->prepare("SELECT username FROM users WHERE BINARY username = ? ");
    $stmt->bindParam(1,$username);
    $stmt->execute();   
    if($stmt->rowCount() == 1){
        echo "Sorry username already exists";     
        return false;
    } else{
        return true;
      } 
    }
}//end of checkUsername

It gives me a Parse error: syntax error, unexpected T_IF on line 26.

Usually errors like this happen one line above and not on the actual line number itself.

Seems you don't have a closing semi-colon in

$exceptions= "!@~`#$%^&*()+=/\,;:[]{  }|?,<>"

Replace it with

$exceptions= "!@~`#$%^&*()+=/\,;:[]{  }|?,<>";

上一行缺少分号

$exceptions= "!@~`#$%^&*()+=/\,;:[]{  }|?,<>";

!===大概应该是!==

Quite a few things are broken here.

First of all, strpos matches a whole string, not any included character. You want to use preg_match here

Secondly, your comparator should be !== false, not !=== false, but by using preg_match, you won't need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM