Home  >  Article  >  Backend Development  >  php determines whether the password is simple

php determines whether the password is simple

angryTom
angryTomOriginal
2019-10-17 16:00:554056browse

php determines whether the password is simple

php Determine whether the password is simple

Principle: Regular expression

/((^[0-9]{6,})|(^[a-z]{6,})|(^[A-Z]{6,}))$/ 弱密码
/((^[0-9,a-z]{6,})|(^[0-9,A-Z]{6,})|(^[a-z,A-Z]{6,}))$/  中密码
/^[\x21-\x7e,A-Za-z0-9]{6,}/   强密码

The code is as follows

/**
* 检测密码强度
* @param string $pw 密码
* @return int
*/
function _checkPwLevel($pw){
	if(empty($pw)){
		return 0;
	}
	$pattern['weak'] = '/((^[0-9]{6,})|(^[a-z]{6,})|(^[A-Z]{6,}))$/';
	$pattern['middle'] = '/((^[0-9,a-z]{6,})|(^[0-9,A-Z]{6,})|(^[a-z,A-Z]{6,}))$/';
	$pattern['strong'] = '/^[\x21-\x7e,A-Za-z0-9]{6,}/';
	$key ='';
	foreach($pattern as $k=>$v){
		$res = preg_match($v,$pw);
		if($res){
			$key = $k;
			break;
		}
	}
	switch ($key){
		case 'weak':
		return 3;
		case 'middle':
		return 2;
		case 'strong':
		return 1;
		default:
		return 0;
	}
}

Test:

$r = _checkPwLevel('123465');
echo $r;
$r = _checkPwLevel('abc1345678');
echo $r;
$r = _checkPwLevel('123456...');
echo $r;

Result:

321
// 强 中 弱

More For PHP related knowledge, please visit PHP中文网!

The above is the detailed content of php determines whether the password is simple. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]