note 8364 added to count_chars

From: Date: Fri, 08 Sep 2000 06:09:39 +0000
Subject: note 8364 added to count_chars
Groups: php.notes 
Request: Send a blank email to php-notes+get-4498@lists.php.net to get a copy of this message
Usage Example Using count_chars() to figure out which characters are in a string, and how many instances of each character were present: <? $data = "Two Ts and one F."; $result = (count_chars($data, 0)); for ($i=0; $i < (sizeof($result)); $i++){ if ($result[$i]) echo "There were $result[$i] instances of \"" . (chr($i)) ."\" in the string. <br>"; } } ?> The results would be: There were 4 instances of " " in the string. There were 1 instances of "." in the string. There were 1 instances of "F" in the string. There were 2 instances of "T" in the string. There were 1 instances of "a" in the string. There were 1 instances of "d" in the string. There were 1 instances of "e" in the string. There were 2 instances of "n" in the string. There were 2 instances of "o" in the string. There were 1 instances of "s" in the string. There were 1 instances of "w" in the string. Application Example Using count_chars() to perform one check on the contents of a potential password string: if any character appears more than 5 times in the string, reject the password as unsafe. <? # $password is passed to the script on runtime $result = (count_chars($password, 0)); for ($i=0; $i < (sizeof($result)); $i++){ if ($result[$i] > 5) die("Your password contained too many instances of the same character. Please go back and choose a more random password."); } ?> Hope these help! http://www.php.net/manual/function.count-chars.php

« previous php.notes (#4498) next »