remove duplicate from string in PHP

The shortest code would be:

$str = implode(',',array_unique(explode(',', $str)));

If it is the fastest… I don’t know, it is probably faster then looping explicitly.

Reference: implode, array_unique, explode

Leave a Comment