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
Post a Comment