PHP 字母数字正则表达式

标签 php regex

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

if (!preg_match("!^[\<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bec9fe9093" rel="noreferrer noopener nofollow">[email protected]</a>]*$!")

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

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

好像不行,有谁可以帮忙吗?如果您感到困惑,或者措辞不好,请询问,我会重写它:)

更新

我的表情:

if (!preg_match("!^[\<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="463106686b" rel="noreferrer noopener nofollow">[email protected]</a>]*$!")

确实有效,但另一个无效。谢谢你! :)

更新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
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }   
?>

最佳答案

Regex to accept all alphanumeric characters, and the symbols @.+-.

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

您的正则表达式 ^[\<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="661126484b" rel="noreferrer noopener nofollow">[email protected]</a>]*$不会匹配重音字符。即\w仅匹配英文字母、数字 0-9加下划线符号。

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

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

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

DEMO

更新:

您需要包含 unicode 修饰符 u ,这样就可以得到 \p{L}匹配重音字符。

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

关于PHP 字母数字正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27710452/

相关文章:

java - PHP 网站上的 map 放置编辑器

javascript - 正则表达式允许所有数字但如果在 Javascript 中只有 0 则不允许

c# - 从 Div 标签中提取内容 C# RegEx

python - 如何使用正则表达式匹配请求行?

javascript - String.match() 函数即使匹配也返回 null

python - 使用正则表达式从字符串列表中提取特定信息

php - 完成 Paypal 购买后关闭主题

php - 登录后的 Paypal API 重定向到空白屏幕

php - 似乎无法将 CSV 数据插入数据库

php - 如何将以逗号分隔的数据添加到数据库