Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

469
Visualizações
How to remove duplicate words from string using php?

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?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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)));
about 4 years ago · Santiago Trujillo Relatório

0

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);
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda