RE: [PHP] User Passwords: checking for unique chars

From: Date: Mon, 14 Feb 2005 16:45:45 +0000
Subject: RE: [PHP] User Passwords: checking for unique chars
Groups: php.general 
Request: Send a blank email to php-general+get-208648@lists.php.net to get a copy of this message
From: Alex Gemmell [mailto:agemmell@gmail.com] > Hello! > > I'm checking user chosen passwords for validity and have created 7 > tests. It's not 100% bulletproof but it will do for now. My problem > is with the last check "have 6 unique characters". I'm at a loss at > how to check for this in a neat one-liner. > > My brain is starting to go off on some horribly complicated routines > but I'm sure it can be done neatly (like the regular expressions). > Can anyone help me with this? By the way - I've only just learnt > regular expressions this morning so I'm no expert on them... The quick & dirty way, I think, would be the following: <? // create an assoc. array of the chars $pass_chars = array(); for ($i = 0; $i < strlen($password); $i++) { $pass_chars[substr($password, $i, 1)]++; } // array keys are the # of unique chars if (count($pass_chars) < 6) return FALSE; ?> I'm curious to see if there's an easier way to do it, though. I'm not sure regexps are the answer. -- Mike Johnson Smarter Living, Inc. Web Developer www.smartertravel.com mike@smarterliving.com (617) 886-5539

« previous php.general (#208648) next »