php - String comparison regardless of case -
i'm trying find different variations of "username" or "password" , shown below, in case-insensitive manner:
$unvar1 = "username"; $unvar2 = "user name"; $usernamevariations1 = strcasecmp($unvar1, $unvar2); $unvar3 = "user"; $unvar4 = "id"; $usernamevariations2 = strcasecmp($unvar3, $unvar4); $pwvar1 = "password"; $pwvar2 = "pass"; $passwordvariations1 = strcasecmp($pwvar1, $pwvar2); if ($element->value === $usernamevariations1 || $element->value === $usernamevariations2 || $element->value === $passwordvariations1) { echo "weee!"; } else { echo "boo!"; }
the problem outputs "boo
" each element in foreach()
output. doing wrong? possible set of these values in array? thanks.
you're making more complicated needs be. if usernames , passwords not case sensitive, create them lowercase when compare them:
if (strtolower($username) === strtolower($element->value)) { // ok }
now if you're allowing spaces added middle, , abbreviations, can seek plan b:
$valid_usernames = array('username', 'username', 'user name', 'use nam'); if (in_array($element->value, $valid_usernames)) { // ok }
keep in mind responsible keeping $valid_usernames
complete.
php string-matching case-insensitive
No comments:
Post a Comment