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 - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -