正则表达式不工作密码?不知道为什么

时间:2022-11-15 13:23:19

So I have a regex code to make sure the password is from 4 to 13 characters, it keeps failing

所以我有一个正则表达式代码,以确保密码是4到13个字符,它一直失败

public function valid_password($password){
        if(preg_match('^.{3,14}^', $password)){
            return true;
        }//end if
        else{
            return false;
        }//end else     
    } 

I'm using regex and not php's length attribute because I will be adding more regex code later on. However, now I'm stuck with this failing program.

我正在使用正则表达式而不是php的长度属性,因为我稍后会添加更多的正则表达式代码。但是,现在我坚持这个失败的程序。

The problem is, it makes sure the password is over 3 characters however it doesn't care how long it is as if I have set no limit.

问题是,它确保密码超过3个字符,但它不关心它有多长,好像我没有设置限制。

Thanks in advance

提前致谢

5 个解决方案

#1


1  

Try this:

尝试这个:

preg_match('/^.{3,14}$/', $password)

#2


2  

You have used ^ as delimiters php.net/mregexp.reference.delimiters.php here:

你在这里使用了^ as delimiters php.net/mregexp.reference.delimiters.php:

^.{3,14}^

That's possible, but not a good idea in your case, because you need the ^ for its actual purpose. It matches the start of the subject normally. And to correctly count the length, yout need to do that. You also need the $ end of subject meta character.

这是可能的,但在你的情况下不是一个好主意,因为你需要^为它的实际目的。它通常匹配主题的开头。要正确计算长度,你需要这样做。你还需要主题元字符的$ end。

So basically:

所以基本上:

preg_match('/^.{3,14}$/'

Now if you want to combine this regex with other match criteria, I would recommend this fancy assertion syntax:

现在,如果您想将此正则表达式与其他匹配条件相结合,我建议使用这种花哨的断言语法:

preg_match('/(?=^.{3,14}$) (...)/x', $password)

This will allow you to specify something else in place of ... - and keep the length check implicit.

这将允许您指定其他代替... - 并隐藏长度检查。

#3


2  

^ is the anchor for the beginning of a string. The end of a string is delimited using $:

^是字符串开头的锚点。字符串的结尾使用$分隔:

public function valid_password($password) {
    return preg_match('~^.{3,14}$~', $password);
}

But in this case I wouldn't use a regex, but the strlen function instead:

但在这种情况下我不会使用正则表达式,而是使用strlen函数:

public function valid_password($password) {
    $length = strlen($password);
    return $length >= 3 && $length <= 14;
}

If you like hacking around to save you that line:

如果你喜欢黑客来拯救你那条线:

public function valid_password($password) {
    return isset($password[2]) && !isset($password[14]);
}

But really, why do you want to restrict password length to 14 characters? That way you prevent people from choosing really secure passwords. You probably should raise that limit.

但实际上,为什么要将密码长度限制为14个字符?这样就可以防止人们选择真正安全的密码。你可能应该提高这个限制。

#4


1  

try

尝试

preg_match('/^.{3,14}$/', $password)

but regexp for counting string length?? = Overkill

但正则表达式计算字符串长度? =矫枉过正

#5


1  

^ matches the start of a string, not the end. $ matches the end.

^匹配字符串的开头,而不是结尾。 $匹配结束。

And you forgot your delimiters.

而你忘了你的分隔符。

The full, fixed line is:

完整的固定线是:

if (preg_match('/^.{3,14}$/', $password)) {

However, I strongly recommend that you instead use:

但是,我强烈建议您改为使用:

$len = strlen($password);
if ($len >= 3 && $len <= 14) {

instead, since regular expressions are completely overkill for this.

相反,因为正则表达式对此完全过度。

#1


1  

Try this:

尝试这个:

preg_match('/^.{3,14}$/', $password)

#2


2  

You have used ^ as delimiters php.net/mregexp.reference.delimiters.php here:

你在这里使用了^ as delimiters php.net/mregexp.reference.delimiters.php:

^.{3,14}^

That's possible, but not a good idea in your case, because you need the ^ for its actual purpose. It matches the start of the subject normally. And to correctly count the length, yout need to do that. You also need the $ end of subject meta character.

这是可能的,但在你的情况下不是一个好主意,因为你需要^为它的实际目的。它通常匹配主题的开头。要正确计算长度,你需要这样做。你还需要主题元字符的$ end。

So basically:

所以基本上:

preg_match('/^.{3,14}$/'

Now if you want to combine this regex with other match criteria, I would recommend this fancy assertion syntax:

现在,如果您想将此正则表达式与其他匹配条件相结合,我建议使用这种花哨的断言语法:

preg_match('/(?=^.{3,14}$) (...)/x', $password)

This will allow you to specify something else in place of ... - and keep the length check implicit.

这将允许您指定其他代替... - 并隐藏长度检查。

#3


2  

^ is the anchor for the beginning of a string. The end of a string is delimited using $:

^是字符串开头的锚点。字符串的结尾使用$分隔:

public function valid_password($password) {
    return preg_match('~^.{3,14}$~', $password);
}

But in this case I wouldn't use a regex, but the strlen function instead:

但在这种情况下我不会使用正则表达式,而是使用strlen函数:

public function valid_password($password) {
    $length = strlen($password);
    return $length >= 3 && $length <= 14;
}

If you like hacking around to save you that line:

如果你喜欢黑客来拯救你那条线:

public function valid_password($password) {
    return isset($password[2]) && !isset($password[14]);
}

But really, why do you want to restrict password length to 14 characters? That way you prevent people from choosing really secure passwords. You probably should raise that limit.

但实际上,为什么要将密码长度限制为14个字符?这样就可以防止人们选择真正安全的密码。你可能应该提高这个限制。

#4


1  

try

尝试

preg_match('/^.{3,14}$/', $password)

but regexp for counting string length?? = Overkill

但正则表达式计算字符串长度? =矫枉过正

#5


1  

^ matches the start of a string, not the end. $ matches the end.

^匹配字符串的开头,而不是结尾。 $匹配结束。

And you forgot your delimiters.

而你忘了你的分隔符。

The full, fixed line is:

完整的固定线是:

if (preg_match('/^.{3,14}$/', $password)) {

However, I strongly recommend that you instead use:

但是,我强烈建议您改为使用:

$len = strlen($password);
if ($len >= 3 && $len <= 14) {

instead, since regular expressions are completely overkill for this.

相反,因为正则表达式对此完全过度。