2015-06-06 48 views
1

所以我想從HTML表單中獲取電子郵件地址。當我在PHP中收到電子郵件如何檢查字符串中是否只允許特殊字符(@, _, -, and .)?例如:電子郵件輸入!#[email protected]PHP只允許@,_, - 和。從輸入

$input = !#[email protected]; 
if(check is only allowed chars in $input string){ 
    //there is only allowed and check is ok 
} else { 
    //there is not only allowed check and do something else 
} 
+3

爲什麼不能代替FILTER_VALIDATE_EMAIL? – mario

回答

-2

如果你只需要驗證電子郵件,你有很多選擇來驗證它:

一個。使用HTML5輸入字段進行電子郵件

<input type="email" name="email" /> 

b。 FILTER_VALIDATE_EMAIL

+1

如果選項a是用戶友好的但不安全的驗證,因爲此驗證是瀏覽器且容易繞過。由於這個原因,您應該始終驗證serverside。 –

+0

使用HTML5「電子郵件」不是驗證電子郵件的(單一)選項。數據應該始終在服務器端進行驗證。 'Filter_validate_email'是做這件事最正確的功能。 – JayTaph

+1

我總是使用選項a,並且我搜索了選項b,現在我使用它們都是100%確定的謝謝。 – Flyer

-1

拳你找到的特殊字符存在的字符串,然後執行檢查,只允許特定的字符

if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $input)) 
    { 

     if (strpos($input,'@') === true || strpos($input,'-') === true strpos($input,'_') === true strpos($input,'.') === true) { 

     // do something 

    } 
    } 
0
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) 
{ 
    $errors['email'] = 'This is not a valid email'; 
}