dragonpeng200811111 2017-03-21 20:24
浏览 32
已采纳

在PHP中匹配RegEx时发出警告[重复]

This question already has an answer here:

I need to validate a password matching following criteria:

  1. At least one small alphabet
  2. At least one capital alphabet
  3. At least one digit
  4. At least one of these special symbols - . _ @ # $ &
  5. Having 8 - 15 characters length

For this I found one somewhat matching RegEx on Internet and modified it as per my requirement.

<?php
   $pwd = "Abcdef.1";
   if (preg_match("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_.@#$&]).{8,15}$", $pwd)){
       echo 'Match Found';
   }
   else{
       echo 'No Match Found';
   }
?>

While executing the script, PHP throws a warning: No Match FoundPHP Warning: preg_match(): No ending delimiter '^' found in [my_file_path].php on line 3

Any help please?

</div>
  • 写回答

1条回答 默认 最新

  • doumu6941 2017-03-21 20:26
    关注

    You need to enclose the pattern into "//" like so:

    preg_match("/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_.@#$&]).{8,15}$/", $pwd)
    

    EDIT:

    Ok so i would actually split the big and confusing regex line into multiple regex pattern, one for each of the constraints you have, it will be easier to read and understand, and also you could give the user more descriptive hint to whats the problem with the currently entered password ( is it too short, is it missing symbol, is it missing uppercase letter.. you got the point..)

    Here is the code:

    <?php
    
    function validatePassword($pass)
    {
    
        $uppercasePattern = "/[A-Z]/";
        $lowercasePattern = "/[a-z]/";
        $symbolPattern =  "/[\.\$@_#&]/";
        $digitPattern = "/[0-9]/";
        $unwantedSymbolsPattern = "/[^\w\.\$@_#&]/";
    
        $len = strlen($pass);
    
        if($len >=8 && $len <= 15){ // check if length is 8 - 15
            $hasUppercase = false;
            preg_match($uppercasePattern, $pass, $hasUppercase); // check if has uppercase letter
    
    
            if(!empty($hasUppercase)){
                $hasLowercase = false;
                preg_match($lowercasePattern, $pass, $hasLowercase); // check if has lowercase letter
    
    
                if(!empty($hasLowercase)){
                    $hasSymbol = false;
                    preg_match($symbolPattern, $pass, $hasSymbol); // check if has symbol
    
    
                    if(!empty($hasSymbol)){
                        $hasDigit = false;
                        preg_match($digitPattern, $pass, $hasDigit); // check if has digit
    
    
                        if(!empty($hasDigit)){ // check if contains needed symbol
                            $hasUnwantedSymbols = false;
                            preg_match($unwantedSymbolsPattern, $pass, $hasUnwantedSymbols);
    
                            if(empty($hasUnwantedSymbols)){ // if doesnt contain unwanted symbols return true
                                return true;
                            }else{
                                echo "Password contains some of the unwanted symbols!";
                            }
                        }else{
                            echo "Missing digit!";
                        }
                    }else{
                        echo "Missing on of the following symbols: . _ @ $ # &";
                    }
                }else{
                    echo "Missing uppercase letter!";
                }
            }else{
                echo "Missing uppercase letter!";
            }
        }
        else{
            echo "Password length not OK!";
        }
    }
    
    
    
    $pass = "Abcdef@1as#(";
    
    // call the function to test it out
    if(validatePassword($pass)){
        echo "OK";
    }
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端