DragonStyle DragonStyle - 9 months ago 35
PHP Question

PHP: Count different numbers in string

I got a String like this:


How can I count how many diffrent numbers are there?

//remove the starting from the given string,
$input = substr($input, strpos(',', $input) + 1);

//split the string on ',' and put it in an array
$numbers = explode(',', $input);

//an array to put our unique numbers in
$counter = array();
//for each number
foreach($numbers as $number) {
  //check if it is not in the unique 'counter' array
  if(!in_array($number, $counter)) {
    //remember this unique number
    $counter[] = $number;

//count the unique number array
$different = count($counter);
//give some output
echo "There are " . $different . " unique numbers in the given string";

the input variable should be your text, because your text starts with a ',' we strip it out of our input string