dongpin2969 2014-10-14 16:13
浏览 364

在密码验证器中使用特殊字符

I am trying to make a password validator and have run into a problem. It seems my code won't read special character. below is my code:

    <?php
        $Passwords = array(
        'Pas$word1',
        'Pas$word2',
        'Pas$word3',
        'Pas$word4',
        'pas$word1',
        'PAS$WORD1',
        'Password1',
        'Pas$word',
        'Pas$word 1',
        'Pas$word1234567890',
        'P$wd1');

        function validatePassword($Password)
        {
            if (preg_match("[^!-/0-9A-Za-z]", $Password)==1)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        foreach ($Passwords as $Password)
        {
            if (validatePassword($Password) == false)
            {
                echo "<p>The password <em>$Password</em> does not appear to be valid.</p>";
            }
        }
        echo "<p>Processing has completed.</p>";
    ?>

fixed my array so it has single quotes, everything is now displayed. Current problem is that everything is displayed. What is wrong in my validatePassword function?

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • duancutan4770 2014-10-14 16:16
    关注

    Use single quotes or escape your literal dollar signs with backslashes. Otherwise, $word1 will be interpreted as a variable named word1.

    See http://php.net/manual/en/language.types.string.php.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法