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).
PHP substr_count() Function The substr_count() function counts the number of times a substring occurs in a string. Note: The substring is case-sensitive.
php $string = "aabbbccddd"; $array=array($array); foreach (count_chars($string, 1) as $i => $val) { $count=chr($i); $array[]= $val. ",". $count; } print_r($array); ?>
First, we split the string by spaces in a. Then, take a variable count = 0 and in every true condition we increment the count by 1. Now run a loop at 0 to length of string and check if our string is equal to the word.
echo count( array_unique( str_split( '66615888')));
Demo
Docs:
count()
- Count the number of elements in an arrayarray_unique()
- Find the unique elements in an arraystr_split()
- Split a string into an arrayIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With