Howard
Howard

Reputation: 3758

Remove all duplicate characters in a string?

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";

Upvotes: 6

Views: 13363

Answers (1)

nickb
nickb

Reputation: 59709

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

Upvotes: 25

Related Questions