2013-11-26 39 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,你不需要它。

相關問題