php array count includes trailing comma -


i have array exploded url (using $_get). elements in url separated commas when count elements result includes final comma. eg: '?list=jonny,sally,bob,' returns '4' when '?list=jonny,sally,bob' returns '3'. can't avoid final comma genrated them automatically need return 3 on both examples. ideas please?? thanks

$list = explode(",", $_get['list']); $listcount = count($list); //$listcount =(int)$listcount -1; //$list[$listcount]=str_replace($list[$listcount],',',''); echo $listcount; 

nb: commented out lines failed attempt remove comma. $list[$listcount] ,ie final array element doesn't seem exist though counted

if want trim commas @ start or end of string, use trim(). if want @ end of string, can use rtrim().

$list = explode(",", $_get['list']); 

to

$list = explode(",", trim($_get['list'], ',')); 

Comments