Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Find number of different letters in a string

Tags:

php

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).
like image 717
jlee Avatar asked Mar 13 '13 16:03

jlee


People also ask

How do I count the number of letters in a string in PHP?

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.

How do I count the number of repeated characters in a string in PHP?

php $string = "aabbbccddd"; $array=array($array); foreach (count_chars($string, 1) as $i => $val) { $count=chr($i); $array[]= $val. ",". $count; } print_r($array); ?>

How do you count the number of occurrences in a string?

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.


1 Answers

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
like image 61
nickb Avatar answered Oct 15 '22 07:10

nickb