Home » Php » PHP – Count number of commas in string

PHP – Count number of commas in string

Posted by: admin July 12, 2020 Leave a comment

Questions:

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”

How to&Answers:

substr_count($my_string, ",")

If you wish to get all the elements between commas as an array, you can always $splitted = explode(",", $my_string)

Answer:

You could use e.g. substr_count(), or explode().