How to remove duplicate words from string using php?

Thiyagu picture Thiyagu · Apr 6, 2017 · Viewed 10.6k times · Source

Below is the variable I have,

$string = 'AAA,BBB,aAA,BbB,AAA,BbB';

I need the unique string result below,

$string = 'AAA,BBB,aAA,BbB';

How to make it unique just like array_unique() function , is there any default String function to remove duplicate string in PHP?

Answer

LF00 picture LF00 · Apr 6, 2017

I don't know if php have such function, but you can process it like this: live demo

$raw = 'AAA,BBB,aAA,BbB,AAA,BbB';
$string = implode(',', array_unique(explode(',', $raw)));