PHP - the number of numbers in a string

How to count the number of times a comma appears on such a line?

A, B, C, D

He must return "3"

+6
php
source share
2 answers

substr_count($my_string, ",")

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

+22
source share

You can use for example. substr_count () or explode () .

+4
source share

All Articles