Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Count number of commas in string

Tags:

php

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"

like image 555
John Links Avatar asked Nov 07 '10 01:11

John Links


2 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)
like image 142
Gabi Purcaru Avatar answered Oct 05 '22 16:10

Gabi Purcaru


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

like image 37
Oliver Charlesworth Avatar answered Oct 05 '22 16:10

Oliver Charlesworth