PHP - Count number of commas in string

php
15,306

Solution 1

substr_count($my_string, ",")

If you wish to get all the elements between commas as an array, you can always

$splitted = explode(",", $my_string)

Solution 2

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

Share:
15,306

Related videos on Youtube

John Links
Author by

John Links

Updated on June 04, 2022

Comments

  • John Links
    John Links almost 2 years

    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"