11

I want to find how many unique characters a string contains. Examples:

"66615888"    contains 4 digits (6 1 5 8).
"12333333345" contains 5 digits (1 2 3 4 5).
3
  • Do you care about the order in which the numbers (characters) are returned? Commented Mar 13, 2013 at 16:47
  • Question as asked is asking for "how many" unique characters, rather than a list of them. Commented Mar 13, 2013 at 17:01
  • @Floris I don't, only need "how many". Commented Mar 13, 2013 at 17:06

5 Answers 5

21
echo count( array_unique( str_split( '66615888')));

Demo

Docs:

  1. count() - Count the number of elements in an array
  2. array_unique() - Find the unique elements in an array
  3. str_split() - Split a string into an array
Sign up to request clarification or add additional context in comments.

4 Comments

+1 But let me ask: Do you know which performs better: your solution or merlin2011's/NikiC's?
@insertusernamehere I dont know. which one is better?
@insertusernamehere - A simple test tells me NikiC's is faster after 1,000,000 iterations. I prefer this way as IMO it is more readable.
Thanks for testing. I thought that creating the array will produce some overhead and a direct string manipulation might be faster. But if this runs only a few times, in the end it's only micro optimization.
11

count_chars gives you a map of char => frequency, which you can sum up with with array_sum:

$count = array_sum(count_chars($str));

Alternatively you can use the 3 mode for count_chars which will give you a string containing all unique characters:

$count = strlen(count_chars($str, 3));

1 Comment

I prefer this answer over the "accepted" one. Creating an array of all the elements is a lot of overhead - count_chars(,3) is the most direct way to get all the unique characters, and the strlen of that is the count - which is what was asked. It gets my vote.
4

PHP has a function that counts characters.

$data = "foobar";
$uniqued = count_chars($data, 3);// return string(5) "abfor"
$count = strlen($uniqued);

Please see the documentation here.

Comments

1

You can use the following script:

<?php
  $str1='66615888';
  $str2='12333333345';
  echo 'The number of unique characters in "'.$str1.'" is: '.strlen(count_chars($str1,3)).' ('.count_chars($str1,3).')'.'<br><br>';
  echo 'The number of unique characters in "'.$str2.'" is: '.strlen(count_chars($str2,3)).' ('.count_chars($str2,3).')'.'<br><br>';
?>

Output:

The number of unique characters in "66615888" is: 4 (1568)

The number of unique characters in "12333333345" is: 5 (12345)


Here PHP string function count_chars() in mode 3 produces an array having all the unique characters.

PHP string function strlen() produces total number of unique characters present.

Comments

0

Here is another version that also works with multibyte strings:

echo count(array_keys(array_flip(preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY))));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.