6

Say I have the following:

$str = "1AAABBCCCDDDDDDD";

How can I remove all the duplicate characters in the string? So it would look like this?

$result = "1ABCD";
Howard
  • 3,648
  • 13
  • 58
  • 86

1 Answers1

25

All you need is count_chars():

$result = count_chars( $str, 3);

With the second parameter $mode set to 3, count_chars() will output:

a string containing all unique characters

You can see from this demo that this produces:

1ABCD
nickb
  • 59,313
  • 13
  • 108
  • 143
  • 3
    This is actually a better solution than the possible duplicate solution provided by Azodious. – Howard Aug 19 '13 at 03:03
  • does this work on special characters? I get a bunch of unicode ��������� trying to sort a list of characters I have. – Austin Burk Nov 24 '15 at 01:28