How to remove duplicate words from string using php? [duplicate]
Asked Answered
S

2

6

Below is the variable I have,

$string = 'AAA,BBB,aAA,BbB,AAA,BbB';

I need the unique string result below,

$string = 'AAA,BBB,aAA,BbB';

How to make it unique just like array_unique() function , is there any default String function to remove duplicate string in PHP?

Sweptwing answered 6/4, 2017 at 10:11 Comment(4)
For some reason I concatenation the String like below, forloop(){ $string .= $a[i].',';} then finally get the result like thatSweptwing
Even with this type of "data" the link will provide you ananswerJin
You'd have too tell PHP what unique meant, otherwise it doesn't understand that you don't mean remove all but one comma, or that you mean unique blocks of letters rather than individual letters: Explode to an array on ,; array_unique(), then implode with a ,Inducement
Looking back at this old page, this question is an Exact Duplicate of Louis' posted closure link. (it faded away, but I am reviving it)Quadrinomial
S
13

I don't know if php have such function, but you can process it like this: live demo

$raw = 'AAA,BBB,aAA,BbB,AAA,BbB';
$string = implode(',', array_unique(explode(',', $raw)));
Sanyu answered 6/4, 2017 at 10:25 Comment(0)
Q
2

For the record, I fully support Kris' method, and that is the way I would choose if this were for my project. However, I would just like to add that there are several additional ways to skin this cat:

Code: (Demo)

$raw = 'AAA,BBB,aAA,BbB,BbB,AAA';

// str_word_count() -> array_unique() -> implode()
echo implode(',',array_unique(str_word_count($raw,1)));
echo "\n";

// str_getcsv() -> array_unique() -> implode()
echo implode(',',array_unique(str_getcsv($raw)));
echo "\n";

// preg_match_all() -> array_unique() -> join()
echo join(',',array_unique(preg_match_all("/[A-Za-z]{3}/",$raw,$m)?$m[0]:array()));
echo "\n";

// preg_split() -> array_unique() -> join()
echo join(',',array_unique(preg_split("/,/",$raw)));
echo "\n";

// preg_replace() -> parse_str() -> implode()
parse_str(preg_replace('/(^|,)([A-Za-z]{3})/',"$2=$2&",$raw),$array);
echo implode(',',$array);

I have 5 different methods to explode the csv string without using explode().

1 method that doesn't use array_unique.

And of course implode() and join() can be used interchangeably as they are synonyms.

I think the fifth method is my favorite as it is the wackiest and doesn't use array_unique(). *unfortunately it's a two-liner :{


p.s.

@Thiyagu says this is how the string is constructed:

forloop(){ $string .= $a[i].',';}

If that is true, then weeding out the duplicates can be done inside this loop by leveraging a temporary array. This has the added benefit of omitting the trailing comma that concatenation generates.

foreach($data as $value){
    $result[$value]=$value;  // duplicate values will be overwritten because arrays may not have two identical keys
}
echo implode(',',$result);
Quadrinomial answered 6/4, 2017 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.