php Preg_match 所有特殊字符,密码检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8053837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:51:18  来源:igfitidea点击:

Preg_match for all special characters, password checking

phppreg-matchspecial-characters

提问by JeffBaumgardt

Ok so I am writing a password checker for our password policy which requires 3 of the 4 major classifications. Where I'm having problems with is the special character match.

好的,所以我正在为我们的密码策略编写密码检查器,它需要 4 个主要分类中的 3 个。我遇到问题的地方是特殊字符匹配。

Here's what I have thus far:

这是我到目前为止所拥有的:

private function PasswordRequirements($ComplexityCount) {
    $Count = 0;
    if(preg_match("/\d/", $this->PostedData['password']) > 0) {
        $Count++;
    }
    if(preg_match("/[A-Z]/", $this->PostedData['password']) > 0) {
        $Count++;
    }
    if(preg_match("/[a-z]/", $this->PostedData['password']) > 0) {
        $Count++;
    }
    // This is where I need help
    if(preg_match("/[~`!@#$%^&*()_-+=\[\]{}\|\:;\"\'<,>.]/", $this->PostedData['password']) > 0) {
        $Count++;
    }

    if($Count >= $ComplexityCount) {
        return true;
    } else {
        return false;
    }
}

So basically what I'm doing is checking the string for each case, numbers, uppercase, lowercase, and special characters. We don't have any restrictions on any special character and I also need unicode characters. Does the \W work in this case or would that also include numbers again? I can't find great documentation on \W so I'm unclear on this part.

所以基本上我正在做的是检查每个大小写、数字、大写、小写和特殊字符的字符串。我们对任何特殊字符没有任何限制,我也需要 unicode 字符。\W 在这种情况下是否有效,或者是否还会再次包含数字?我在 \W 上找不到很好的文档,所以我不清楚这部分。

Does anyone know of a easy regexp that would cover all special characters and unicode characters that does not include numbers and letters?

有谁知道一个简单的正则表达式可以涵盖所有特殊字符和不包括数字和字母的 unicode 字符?

Anyone is free to use this as I think more than a few people have been looking for this.

任何人都可以自由使用它,因为我认为有很多人一直在寻找它。

回答by Marcus

This pattern would allow all characters that's not a digit or a-Z.

这种模式将允许所有不是数字或 aZ 的字符。

[^\da-zA-Z]

Regarding the \Wit's a negated \w, which is the same as [A-Za-z0-9_]. Thus will \Wbe all characters that's not an english letter, digit or an underscore.

关于\W它是一个否定的\w,它与[A-Za-z0-9_]. 因此将\W是不是英文字母、数字或下划线的所有字符。

As I mentioned as a comment this is a great resourcefor learning regex. And here's a good site to test the regex.

正如我在评论中提到的,这是学习正则表达式的绝佳资源。这是一个测试 regex的好网站。

回答by pmm

In case you want to match on special characters

如果您想匹配特殊字符

preg_match('/[\'\/~`\!@#$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\]/', $input)

回答by DTest

You can use the POSIX character class [[:punct:]]for the 'special' characters:

您可以将 POSIX 字符类[[:punct:]]用于“特殊”字符:

<?php
$regex = '[[:punct:]]';

if (preg_match('/'.$regex.'/', 'somepas$', $matches)) {
    print_r($matches);
}
?>

gives:

给出:

Array
(
    [0] => $
)