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";
    }
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗