How to count instances of characters in a string with PHP

1 Answer

0 votes
$s = "PHP Programming";

foreach (count_chars($s, 1) as $i => $val) 
    echo "$val instance(s) of \"" . chr($i) . "\""  . "<br />";


/*
run: 

1 instance(s) of " "
1 instance(s) of "H"
3 instance(s) of "P"
1 instance(s) of "a"
2 instance(s) of "g"
1 instance(s) of "i"
2 instance(s) of "m"
1 instance(s) of "n"
1 instance(s) of "o"
2 instance(s) of "r"

*/

 



answered Jun 5, 2016 by avibootz
...