note 8364 deleted from function.count-chars by didou
| From: | didou@php.net | Date: | Mon, 20 Oct 2003 11:56:26 +0000 |
| Subject: | note 8364 deleted from function.count-chars by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58935@lists.php.net to get a copy of this message | ||
Note Submitter: shaun@phpboards.com
----
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!