php 如何检查字符串在php中是否至少有一个字母、数字和特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9587907/
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
How to check if string has at least one letter, number and special character in php
提问by Peter Stuart
I am currently writing a small script that checks the contents of each string.
我目前正在编写一个检查每个字符串内容的小脚本。
I was wondering what the REGEX would be to make sure that a string has a letter (upper or lower), a digit, and a special character?
我想知道 REGEX 是什么来确保一个字符串有一个字母(大写或小写)、一个数字和一个特殊字符?
Here is what I know so far (whcih isn't much):
这是我到目前为止所知道的(whcih并不多):
if(preg_match('/^[a-zA-Z0-9]+$/i', $string)):
Help would be great!
帮助会很棒!
Thank You!
谢谢你!
回答by Ry-
The easiest (and probably best) way is to do three separate checks with preg_match
:
最简单(也可能是最好)的方法是使用以下命令进行三个单独的检查preg_match
:
$containsLetter = preg_match('/[a-zA-Z]/', $string);
$containsDigit = preg_match('/\d/', $string);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $string);
// $containsAll = $containsLetter && $containsDigit && $containsSpecial
回答by Igor Korkhov
回答by Emad Samir Zaki
I have found great answer here with explanation to make sure that a given string contains at least one character from each of the following categories.
我在这里找到了很好的答案,并附有说明,以确保给定的字符串至少包含来自以下每个类别的一个字符。
Lowercase character, Uppercase character, Digit, Symbol
小写字符、大写字符、数字、符号
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$
A short explanation:
一个简短的解释:
^
// the start of the string
^
// 字符串的开头
(?=.*[a-z])
// use positive look ahead to see if at least one lower case letter exists
(?=.*[a-z])
// 使用正向预测来查看是否至少存在一个小写字母
(?=.*[A-Z])
// use positive look ahead to see if at least one upper case letter exists
(?=.*[A-Z])
// 使用正向预测来查看是否至少存在一个大写字母
(?=.*\d)
// use positive look ahead to see if at least one digit exists
(?=.*\d)
// 使用正向预测来查看是否至少存在一位数字
(?=.*[_\W])
// use positive look ahead to see if at least one underscore or non-word character exists
(?=.*[_\W])
// 使用正向预测来查看是否至少存在一个下划线或非单词字符
.+
// gobble up the entire string
.+
// 吞噬整个字符串
$
// the end of the string
$
// 字符串的结尾
Hope that help you.
希望能帮到你。
回答by arc
It may be best to use 3 distinct regexs to do this, since you would need to match 6 different possibilities, depending on where your special characters are in your string. But if you want to do it in one regex, and your special characters are, say, [+?@], it is possible:
最好使用 3 个不同的正则表达式来执行此操作,因为您需要匹配 6 种不同的可能性,具体取决于您的特殊字符在字符串中的位置。但是,如果您想在一个正则表达式中执行此操作,并且您的特殊字符是 [+?@],则可能:
$string = "abc@123";
$regex = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$%^&]).*$/"
if (preg_match($regex, $string)) {
// special characters
}
回答by Toto
A letter is \pL
, a number is \pN
and a special char is [what you want]
, here I assume it is not a letter and not a number, so the regex looks like:
一个字母是\pL
,一个数字是\pN
,一个特殊的字符是[what you want]
,在这里我假设它不是一个字母也不是一个数字,所以正则表达式看起来像:
/^(?=.*?\pL)(?=.*?\pN)(?=.*[^\pL\pN])/
回答by Scott Hallett
False (chosen answer above - Thanks!) had the a pretty easy way to wrap your head around it (if you aren't too familiar with regex) and come up with what works for you.
错误(上面选择的答案 - 谢谢!)有一个非常简单的方法来解决它(如果您不太熟悉正则表达式)并想出适合您的方法。
I just put this in to elaborate a bit:
我只是把这个放在详细说明一下:
(you can paste it at http://phptester.net/index.php?lang=ento work with it)
(您可以将其粘贴到http://phptester.net/index.php?lang=en以使用它)
<?php
$pass="abc1A";
$ucl = preg_match('/[A-Z]/', $pass); // Uppercase Letter
$lcl = preg_match('/[a-z]/', $pass); // Lowercase Letter
$dig = preg_match('/\d/', $pass); // Numeral
$nos = preg_match('/\W/', $pass); // Non-alpha/num characters (allows underscore)
if($ucl) {
echo "Contains upper case!<br>";
}
if($lcl) {
echo "Contains lower case!<br>";
}
if($dig) {
echo "Contains a numeral!<br>";
}
// I negated this if you want to dis-allow non-alphas/num:
if(!$nos) {
echo "Contains no Symbols!<br>";
}
if ($ucl && $lcl && $dig && !$nos) { // Negated on $nos here as well
echo "<br>All Four Pass!!!";
} else {
echo "<br>Failure...";
}
?>