98.8k views
0 votes
Write a php program that checks the elements of a string array named $passwords. use regular expressions to test whether each element is a strong password. for this exercise, a strong password must have at least one number, one lowercase letter, one uppercase letter, no spaces, and at least one character that is not a letter or number. (hint: use the [^0-9a-za-z] character class.) th e string should also be between 8 and 16 characters long.

User Furins
by
8.0k points

1 Answer

4 votes

<?php

function pass_strong($candidate) {

if (!preg_match_all('$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S [\W])\S*$', $candidate))

return FALSE;

return TRUE;

}

/*

Explaining

$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$

$ = string beginning

\S* = characters set

(?=\S{8,}) = with length atleast 8

(?=\S*[a-z]) = with at least 1 letter that is lowercase

(?=\S*[A-Z]) = with at least 1 letter that is uppercase

(?=\S*[\d]) = with at least 1 number

(?=\S*[\W]) = with at least 1 special character

$ = string end

*/

$Passwords = 'password01';

if(pass_strong($Passwords))

echo "$Passwords is a strong password<br />";

else echo "$Passwords is NOT a strong password<br />";

?>

User Yogesh Cl
by
7.5k points