Possible Duplicate:
remove duplicate from string in PHP
Have a string like:
$str = "1 - 1 - 2 - 3 - 1 - 4 - 3 - 1 - 2 - 5 - 6 - 4";
and i need to get
$str = "1 - 2 - 3 - 4 - 5 - 6";
any help.. :(
Possible Duplicate:
remove duplicate from string in PHP
Have a string like:
$str = "1 - 1 - 2 - 3 - 1 - 4 - 3 - 1 - 2 - 5 - 6 - 4";
and i need to get
$str = "1 - 2 - 3 - 4 - 5 - 6";
any help.. :(
Example/Demo:
$elements = array_unique(explode(' - ', $input));
sort($elements);
$str = implode(' - ', $elements);
I've just had a quick look around and with the code from
remove duplicate from string in PHP
$str = implode(' - ',array_unique(explode(' - ', $string )));
This would work for you.
You can see a demo of the example.
Something like this:
<?php
$str = str_replace(' - ', ',', $str);
$items = array_unique(explode(',', $str));
echo implode(' - ', $items);
?>
I've swapped the space-hifen-space delimiter so you can see what's going on in intermediate steps - but you can explode on that immediately if you wish.
If you have to make new string with unique values you would try this:
implode(' - ', array_unique(explode(' - ', $array)));
yeah, I would do something like this:
$strArray = explode(' - ',$str);
str = array_unique($strArray);
Or something to that effect.