PHP - Count number of commas in string
How can I count the number of times a comma appears in a string 开发者_开发知识库such as this?
A,B,C,D
It should return "3"
substr_count($my_string, ",")
If you wish to get all the elements between commas as an array, you can always
$splitted = explode(",", $my_string)
You could use e.g. substr_count(), or explode().
$str = "A,B,C,";
echo (string)(count(explode(",",$str))-1);
count
array_explode
精彩评论