我目前正試圖弄清楚如何檢查並查看$ _POST ['email']是否包含使用php的特定字符串。
我目前試圖
}elseif(empty($_POST['email']) || !strpos('@gmail.com', $_POST['email'])) {
輸出表示,對strpos不包含的結局,當它。我現在做錯了什麼,還是有另一種方法呢?
我只希望人們能夠使用gmail進行註冊,而不是其他任何未知的電子郵件帳戶用於垃圾郵件使用。
我目前正試圖弄清楚如何檢查並查看$ _POST ['email']是否包含使用php的特定字符串。
我目前試圖
}elseif(empty($_POST['email']) || !strpos('@gmail.com', $_POST['email'])) {
輸出表示,對strpos不包含的結局,當它。我現在做錯了什麼,還是有另一種方法呢?
我只希望人們能夠使用gmail進行註冊,而不是其他任何未知的電子郵件帳戶用於垃圾郵件使用。
你可能最好關閉使用preg_match
與\b
模式匹配,而不是strpos
。另外,strpos
將匹配GMAIL
如果您不需要大寫字母,則您希望使用stripos
,而不是將其與匹配作爲區分大小寫,FYI。
使用strpos
將匹配gmail.comm
而不是預期gmail.com
,我相信你不想要。用戶可以很好地輸入[email protected]
並且仍然被認爲是有效的,我確信你不想要的其他東西,在嘗試回覆郵件或使用自動回覆器時會失敗。
「的模式中的\ b表示一個字邊界,因此只有不同 *單詞 「web」 匹配時,和不偏像「帶子」或「蜘蛛網」一詞
這將不匹配@ gMAIL.com或@ GMAIL.comm但將匹配@ gmail.com,但不 @ gmail.comm
<?php
if(isset($_POST['submit'])) {
$contains = "@gmail.com";
$email = $_POST['email'];
// or use (as outlined below)
// if(isset($_POST['email']) && preg_match("/\b(@gmail.com)\b/", $_POST['email']))
if(isset($_POST['email']) && preg_match("/\b($contains)\b/",$email)) {
echo 'yey! gmail!';
} else {
echo 'invalid input!';
}
}
?>
<form method="POST">
<input type="text" name="email" />
<input type="submit" name="submit" />
</form>
你也可以只做到以下幾點,更換什麼你目前正在使用:
if(isset($_POST['email']) && preg_match("/\b(@gmail.com)\b/", $_POST['email']))
國際海事組織,這是好得多,無論如何,我希望當OP回到網上,應該考慮選擇這一個。 – Ghost 2014-10-05 00:42:08
@鬼魂感謝幽靈。 :) – 2014-10-05 01:03:57
感謝@Fred -ii-今天的幫助,以及我今天想出我的錯誤和困惑。使用preg_match,因爲你做了更多更精確的方法來做到這一點,爲我今後可能發生的事情節省了很多麻煩。 – 2014-10-05 05:16:18
如果您想嘗試,而不是strpos
別的東西,你可以這樣做:
if (empty($_POST['email'] || array_pop(explode('@', $email)) != "gmail.com") {
// Do something if the email is empty or the domain is not "gmail.com"
}
你應該使用'strpos(..)=== FALSE'代替'!strpos(..)',因爲它也可以返回0作爲索引。 – mithunsatheesh 2014-10-04 04:03:56
[Get all $ \ _ POST變量以特定文本開頭](http://stackoverflow.com/questions/8825001/get-all-post-variables-starting-with-certain-text)可能重複。 – jww 2014-10-04 04:14:09
@ md_5檢查我在下面發佈的內容。你最好使用'preg_match()'來做這件事。 – 2014-10-04 15:39:02