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 -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -