Home > Backend Development > PHP Problem > php determines whether the password is simple

php determines whether the password is simple

angryTom
Release: 2023-02-27 08:06:02
Original
4234 people have browsed it

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,}/   强密码
Copy after login

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;
	}
}
Copy after login

Test:

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

Result:

321
// 强 中 弱
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template