2013-11-26 40 views
0

我做了一个函数,检查注册过程中的某个用户名是否包含某些我不希望用户在其用户名中包含的字符。它回应用户可以使用哪些字符。为什么strpos在这里不起作用?

public function checkUsername($username){  
    if(!empty($username)){ 
     $exceptions= "[email protected]~`#$%^&*()+=/\,;:[]{ }|?,<>" 
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 

它给了我一个解析错误:语法错误,意想不到的T_IF上线26

+2

通常这样的错误发生在上面的一行,而不是实际的行号本身。似乎你在'$ exceptions =“!@〜'#$%^&*()+ =/\,;:[] {} |?,<>”中没有分号 –

回答

1

通常这样的错误发生在上面的一行而不是实际的行号本身。

看来你没有关闭分号在

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

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

不少东西都在这里打破更换。

首先,strpos匹配整个字符串,而不是任何包含的字符。其次,你的比较器应该是!== false,不是!=== false,但是通过使用preg_match,你不需要它。

相关问题