5

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";
rotaercz
  • 3,403
  • 12
  • 51
  • 84

1 Answers1

23

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
  • 58,150
  • 12
  • 100
  • 138
  • 3
    This is actually a better solution than the possible duplicate solution provided by Azodious. – rotaercz 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