php 检查字符串是否包含数字和字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9335915/
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
Check if a string contains numbers and letters
提问by Duncan Palmer
I want to detect if a string contains both numbers and letters.
我想检测一个字符串是否同时包含数字和字母。
For example:
例如:
- Given
PncC1KECj4pPVW
, it would be written to a text file because it contains both. - Given
qdEQ
, it would not, because it only contains letters.
- 鉴于
PncC1KECj4pPVW
,它将被写入文本文件,因为它包含两者。 - 鉴于
qdEQ
,它不会,因为它只包含字母。
Is there a method to do this?
有没有办法做到这一点?
I was trying to use
我试图使用
$string = PREG_REPLACE("/[^0-9a-zA-Z]/i", '', $buffer);
But it didn't work.
但它没有用。
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by Mohammad Naji
if (preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $myString))
{
echo 'Secure enough';
}
Answer updated based on https://stackoverflow.com/a/9336130/315550, thnx to https://stackoverflow.com/users/116286/jb
答案基于https://stackoverflow.com/a/9336130/315550更新,感谢https://stackoverflow.com/users/116286/jb
回答by jb.
It seems the simplest way is just to do it in two regex's.
似乎最简单的方法就是用两个正则表达式来做。
if (preg_match('/[A-Za-z]/', $myString) && preg_match('/[0-9]/', $myString))
{
echo 'Contains at least one letter and one number';
}
I suppose another way to do it is this below. It says "a letter and then later on at some point a number (or vice versa)". But the one above is easier to read IMO.
我想另一种方法是下面这个。它说“一个字母,然后在某个时候是一个数字(反之亦然)”。但是上面的那个更容易阅读 IMO。
if (preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $myString))
{
echo 'Contains at least one letter and one number';
}
回答by Jonathan M
This works cleanly:
这很干净:
$myString="abc123";
if( preg_match('([a-zA-Z].*[0-9]|[0-9].*[a-zA-Z])', $myString) )
{
echo('Has numbers and letters.');
} else {
echo("no");
}
To see it in action, copy it and paste it here: http://phptester.net/index.php?lang=en
要查看它的实际效果,请将其复制并粘贴到此处:http: //phptester.net/index.php?lang=en
回答by Vyktor
My only question is whether is had to be one regexp. I'd go with two or three (because you have to build a little complex regexp to do it at once.
我唯一的问题是是否必须是一个正则表达式。我会选择两三个(因为您必须构建一个复杂的正则表达式才能立即完成。
Let's say that you require to have:
假设您需要:
- at least one upper case character
[A-Z]
- at least one lower case character
[a-z]
- at least one number
\d
- have password at least 7 characters long
- 至少一个大写字符
[A-Z]
- 至少一个小写字符
[a-z]
- 至少一个数字
\d
- 密码长度至少为 7 个字符
The easiest and the most effective solution:
最简单有效的解决方案:
if( preg_match( '~[A-Z]~', $password) &&
preg_match( '~[a-z]~', $password) &&
preg_match( '~\d~', $password) &&
(strlen( $password) > 6)){
echo "Good password";
} else {
echo "Not so much";
}
Otherwise, in one regexp you will have to consider several options:
否则,在一个正则表达式中,您将不得不考虑几个选项:
[a-z][A-Z]+\d
[a-z]\d+[A-Z]
[A-Z][a-z]+\d
[A-Z]\d+[a-z]
\d[a-z]+[A-Z]
\d[A-Z]+[a-z]
[a-z][A-Z]+\d
[a-z]\d+[A-Z]
[A-Z][a-z]+\d
[A-Z]\d+[a-z]
\d[a-z]+[A-Z]
\d[A-Z]+[a-z]
Join it into one big and hardly readable "ored" regexp like:
将它加入一个大而难读的“ored”正则表达式,如:
~([a-z][A-Z]+\d|[a-z]\d+[A-Z]|[A-Z][a-z]+\d|[A-Z]\d+[a-z]|\d[a-z]+[A-Z]|\d[A-Z]+[a-z])~
Of course you can go with (when needing just check upper and lower case):
当然你可以去(当需要只检查大小写):
preg_match( '~([a-z][A-Z]|[a-z][A-Z])~');
And still have to check length manually. The second solution seems pretty ineffective and hard to read to me. My recommendation: go with the first one.
并且仍然必须手动检查长度。第二种解决方案对我来说似乎非常无效且难以阅读。我的建议:选择第一个。
回答by Krii
PHP has a built-in function for this. It's called ctype_alnum
.
PHP 有一个内置函数。它被称为ctype_alnum
。
if(!ctype_alnum($string)) {
return $error;
}
// Continue on your way...
回答by Nikita P
for checking if string contains numbers or not !!
用于检查字符串是否包含数字!!
function ContainsNumbers($String){
return preg_match('/\d/', $String) > 0;
}
回答by David D
If you need to support multiple languages, from PHP 5.1 additional escape sequences were added for the character types listed here.
如果您需要支持多种语言,从 PHP 5.1 开始,为此处列出的字符类型添加了额外的转义序列。
The following functions take advantage of this and allow you to check for letters and numbers in multiple alphabets:
以下函数利用了这一点,并允许您检查多个字母表中的字母和数字:
check any letter exists in string:
检查字符串中是否存在任何字母:
function has_letter($x){
if (preg_match("/[\p{L}]/u",$x)) {
return true;
}
return false;
}
check string contains any number
检查字符串包含任何数字
function has_number($x) {
if (preg_match("/[\p{N}]/u",$x)) {
return true;
}
return false;
}
Usage
用法
$string = '123?????';
has_letter($string); // true
$string = '???';
has_number($string); // true
Both together as requested
根据要求一起
$string='I am 28';
if (has_letter($string)&&has_number($string)){
// true
}
$string='I am ???';
if (has_letter($string)&&has_number($string)){
// true
}
$string='Привет, друг, как ты?';
has_number($string); // false - this Russian text doesn't contain a number
回答by Frosty Z
If you want to match onlyalphanumeric chars (that is, consider the string as invalid as soon as there is anything else into it, like spaces or special characters), this should work.
如果你想匹配只有字母数字字符(即,一旦考虑字符串作为无效的,因为没有别的了进去,像空格或特殊字符),这应该工作。
Otherwise, just remove the first preg_match()
.
否则,只需删除第一个preg_match()
.
function myTest($string)
{
echo "test '".$string."': "
. intval(preg_match('/^[a-z\d]+$/i', $string) // has only chars & digits
&& preg_match('/[a-z]/i', $string) // has at least one char
&& preg_match('/\d/', $string)) // has at least one digit
. "\n";
}
myTest('aAa'); // => 0
myTest('111'); // => 0
myTest('aAa111bbb'); // => 1
myTest('111aAabbb'); // => 1
myTest('aAabbb111'); // => 1
myTest('111bBb222'); // => 1
myTest('111 bBb 222'); // => 0
myTest('$$$$'); // => 0
回答by OM4
Hey here are some guidelines below:
嘿,这里有一些指导方针:
Issue: No validation on the digit inside the string For e.g.
问题:没有对字符串内的数字进行验证例如
OM 4 OM, OM4OM , space OM 4 OM space, space OM4OM space. -----> No valdation over this??
Solution:
解决方案:
if (preg_match("/[A-Za-z].*[0-9]/",$_POST["name"]))
{
header("location:http://localhost/Test1.html");
exit;
}
Logic: /[A-Za-z].*[0-9]/
It says first characters and then at some point numbers.
它先说字符,然后在某些点说数字。