Re: [PHP3] How to count a character in a string

From: Date: Tue, 13 Jun 2000 23:57:59 +0000
Subject: Re: [PHP3] How to count a character in a string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-1780@lists.php.net to get a copy of this message
In article <20000605231124.37032.qmail@hotmail.com>, qniens@hotmail.com ("Da MaN") wrote: > How can I make php3 count a character in a string? > So for example: > How many p's are there in apple? > And then php3 returns 2 in a variable... PHP4 : count_chars() function (or something like that). PHP3: <?php $text = "How many p's are there in apple?"; $len = strlen($text); for ($index = 0; $index < $len; $index++){ $char = $text[$index]; if (!isset($counts[$char])){ $counts[$char] = 0; } $counts[$char]++; } while (list($char, $count) = each($counts)){ echo "$char: $count<BR>\n"; } ?> -- Richard Lynch | If this was worth $$$ to you, buy a CD US Customer Support Director | from one of the artists listed here: Zend Technologies USA | http://www.L-I-E.com/artists.htm http://www.zend.com | (this has nothing to do with Zend, duh!)

« previous php.general (#1780) next »