PHP 字母数字正则表达式


PHP Alphanumeric Regular Expression

我是PHP的新手,对正则表达式也很陌生。我目前正在尝试编写一个正则表达式,以确保字符都是字母数字,并且可以很好地接受其他字符。我想接受áéíñóúüÁÉÍÑÓÚÜ和@.+-。接受所有字母数字字符和符号 @.+-。我使用表达式:

if (!preg_match("!^['w@.-]*$!")

另外,在我继续之前,我想补充一下。这些是从表单输入中获取的值,它们都应该是一个单词短语(没有空格)。好的,现在让我们继续!:D现在我也想抓住字符áéíñóúüÁÉÍÑÓÚÜ。当我使用表达式时:

if (!preg_match("/['wáéíñóúüÁÉÍÑÓÚÜ@.'+-].*/")

它似乎不起作用,有人可以帮忙吗?如果你感到困惑,或者措辞不好,请问,我会重写:)

更新

我的表达:

if (!preg_match("!^['w@.-]*$!")

确实有效,是另一个不起作用。谢谢!:)

更新 2

现在当我尝试表达式时:

if (!preg_match("~^['p{L}'p{N}@+._-]+$~", $email)) {

输入 michael.jonesáéí@gmail.com 时会引发错误。它引发的错误是"电子邮件包含受限制的字符",因为我让它这么说。这是我使用的特定代码:

<?php
if (!preg_match("~^['p{L}'p{N}@+._-]+$~", $email)) {
?>
<div id="allinputboxerror" class="col-xs-12"> Email contains restricted characters.</div>
<?php } ?>  
                                                }

这是我使用的所有代码:

<?php
                                if ($_POST) {
                                    $emailtest1 = False;
                                    $emailtest2 = False;
                                    $emailtest3 = False;
                                    $emailtest4 = False;
                                    $emailtest5 = False;
                                    // Test #1 - Makes sure there is a input
                                    if(empty($_POST['email'])) {
?>
                                        <div id="allinputboxerror" class="col-xs-12"> Please enter your email. </div>
<?php
                                    }
                                    else {
                                        $emailtest1 = True;
                                        // Test #2 - Makes sure it does not already exist
                                        $usernamequery = "SELECT 1 FROM users WHERE email = :email";
                                        $usernameparams = array(':email' => $_POST['email']);
                                        try{
                                            $emailstmt = $connection->prepare($usernamequery);
                                            $emailresult = $emailstmt->execute($usernameparams);
                                        }
                                        catch(PDOException $ex){
                                            echo ("Failed to run query: " . $ex->getMessage());
                                        }
                                        $emailcolumns = $emailstmt->fetch();
                                        if($emailcolumns){
?>
                                            <div id="allinputboxerror" class="col-xs-12"> This email already exists. </div>
<?php
                                        }
                                        else {
                                            $emailtest2 = True;
                                            // Test #3 - Makes sure it fits the length requirements
                                            if(strlen($email) < 5 ) {
?>
                                                <div id="allinputboxerror" class="col-xs-12"> Email is to short. </div>
<?php
                                            }
                                            elseif(strlen($email) > 75 ) {
?>
                                                <div id="allinputboxerror" class="col-xs-12"> Email is to long. </div>
<?php
                                            }
                                            else {
                                                $emailtest3 = True;
                                                // Test #4 - Makes sure it does not have any restricted characters
                                                if (!preg_match("~^['p{L}'p{N}@+._-]+$~", $email)) {
?>
                                                    <div id="allinputboxerror" class="col-xs-12"> Email contains restricted characters. </div>
<?php       
                                                }
                                                else {
                                                    $emailtest4 = True;
                                                    // Test #5 - Makes sure the email is valid
                                                    if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
?>
                                                        <div id="allinputboxerror" class="col-xs-12"> Not a valid email. </div>
<?php       
                                                    }
                                                    else {
                                                        $emailtest5 = True;
                                                        // Final Check
                                                        if (($emailtest1 = True) and ($emailtest2 = True) and ($emailtest3 = True) and ($emailtest4 = True) and ($emailtest5 = True)) {
                                                            // Email is valid! :D
                                                        }
                                                        else {
?>
                                                            <div id="allinputboxerror" class="col-xs-12"> There is a error. </div>
<?php
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }   
?>

正则表达式接受所有字母数字字符和符号 @.+-。

^['p{L}'p{N}@+.-]+$

您的正则表达式^['w@.-]*$与重音字符不匹配。也就是说'w仅匹配英文字母、数字0-9加上下划线符号。

  • 'p{L}匹配来自任何语言的任何类型的字母

  • 'p{N}匹配任何脚本中的任何类型的数字字符。

  • 在 char 类之后+会将前一个标记重复一次或多次。我建议您使用 + 而不是 * *因为重复以前的令牌零次或多次。所以它也匹配空字符串。

演示

更新:

你需要包含 unicode 修饰符 u ,以便它'p{L}匹配重音字符。

if (!preg_match("~^['p{L}'p{N}@+._-]+$~u", $email)) {