Count character X

I am experimenting with something where explode () does not work, so I want to try something else. If I have a string, how can I count the number of characters in it, say comma , as in ,

+1
source share
2 answers

Try substr_count () :

 substr_count($text, ','); 
+3
source

You can use count_chars

An example from the PHP manual:

 $data = "Two Ts and one F."; foreach (count_chars($data, 1) as $i => $val) { echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n"; } 

Output ( codepad ):

 There were 4 instance(s) of " " in the string. There were 1 instance(s) of "." in the string. There were 1 instance(s) of "F" in the string. There were 2 instance(s) of "T" in the string. There were 1 instance(s) of "a" in the string. There were 1 instance(s) of "d" in the string. There were 1 instance(s) of "e" in the string. There were 2 instance(s) of "n" in the string. There were 2 instance(s) of "o" in the string. There were 1 instance(s) of "s" in the string. There were 1 instance(s) of "w" in the string. 
+1
source

All Articles