使用 php 检查密码是否包含简单序列

我想用 php 检查简单的密码模式,比如


123456

123456789

abcde

98764321

101010

202020

是否有可能进行这样的检查而不依赖于维护预定义字符串数组?


长风秋雁
浏览 88回答 1
1回答

ITMISS

只需遍历每个字母并检查下一个字母是否与当前字母相同,少一个或多一个。所以你可以创造一个独特的分数。对于最后一种情况,您可以检查是否有重复字符并从乐谱中删除。$passwords = ['123456', '123456789', 'abcde', '98764321', '101010', '202020', 'xhdgszu'];foreach ($passwords as $password) {&nbsp; &nbsp; $score = $length = strlen($password);&nbsp; &nbsp; $chars = str_split($password);&nbsp; &nbsp; for ($i = 0; $i < $length - 1; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $currentChar = $chars[$i];&nbsp; &nbsp; &nbsp; &nbsp; $nextChar = $chars[$i + 1];&nbsp; &nbsp; &nbsp; &nbsp; if ($currentChar === $nextChar&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || ord($currentChar) - 1 === ord($nextChar)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || ord($currentChar) + 1 === ord($nextChar)&nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $score--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $unique = array_unique($chars);&nbsp; &nbsp; $score -= $length - count($unique);&nbsp; &nbsp; echo "{$password}: {$score} / {$length} ", ($score < $length) ? 'Bad' : 'Good', PHP_EOL;}123456: 1 / 6 Bad123456789: 1 / 9 Badabcde: 1 / 5 Bad98764321: 2 / 8 Bad101010: -3 / 6 Bad202020: 2 / 6 Badxhdgszu: 7 / 7 Good
打开App,查看更多内容
随时随地看视频慕课网APP