php regex how to remove duplicate strings -


here long string like"abc,adbc,abcf,abc,adbc,abcf"

i want use regex remove duplicate strings seperated comma

the following codes, result not expect.

$a='abc,adbc,abcf,abc,adbc,abcf'; $b=preg_replace('/(,[^,]+,)(?=.*?\1)/',',',','.$a.','); echo $b; 

output:,adbc,abc,adbc,abcf,

it should : ,abc,adbc,abcf,

please point problem. thanks.

you can tyr this-

$a='abc,adbc,abcf,abc,adbc,abcf';  $pieces = explode(",", $a); $unique_values = array_unique($pieces); $string = implode(",", $unique_values); 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -