In this post, we are going to see how to write a small piece of PHP snippet to remove all duplicate characters in a string.
This can be achieved with the help of an inbuilt PHP function count_chars()
Let’s see a simple example:
$str = "AABBCCCCCCCCDEEEEFFFFGGGG";
$out = count_chars($str, 3);
Basically, the second parameter in the count_chars() function tells the PHP to remove the duplicate characters from a string.
There are few other options as well
- 0 – an array with the byte-value as key and the frequency of every byte as value.
- 1 – same as 0 but only byte-values with a frequency greater than zero are listed.
- 2 – same as 0 but only byte-values with a frequency equal to zero are listed.
- 3 – a string containing all unique characters is returned.
- 4 – a string containing all not used characters is returned.
You can refer here for more details: http://php.net/manual/en/function.count-chars.php