开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜