检查并查看$ _POST是否包含特定字符串PHP

时间:2014-10-04 03:58:35

标签: php

我目前正在试图弄清楚如何查看$ _POST ['电子邮件']是否包含使用php的特定字符串。

我目前正在尝试

}elseif(empty($_POST['email']) || !strpos('@gmail.com', $_POST['email'])) {

输出表明strpos不包含结尾,当它确实时。我目前做错了什么,或者有另一种方法吗?

我只希望人们能够注册gmail,而不是任何其他未知的电子邮件帐户才能使用垃圾邮件。

2 个答案:

答案 0 :(得分:1)

如果你想尝试别的东西,而不是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"
}

答案 1 :(得分:1)

您可能最好使用preg_match模式匹配而不是\b来使用strpos。另外,如果您不想要大写字母,strpos将匹配GMAIL,您可能希望使用stripos代替它,将其视为不区分大小写,一个FYI

使用strpos将匹配gmail.comm而不是预期的gmail.com,我确定您不想要。{1}}用户可以很好地输入email@gmail.comm并仍然被认为是有效的,我确定您不想要的其他内容,无论是尝试回邮还是使用自动回复都会失败。< / p>

  

&#34;模式中的\ b表示单词边界,因此只有不同的单词边界    * word&#34; web&#34;匹配,而不是像#34; webbing&#34;或&#34; cobweb&#34;

这与@ 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']))

相关问题