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

372
Visualizações
Clean up a comma-separated list by regex

I want to clean up a tag list separated by comma to remove empty tags and extra spaces. I came up with

$str='first , second ,, third, ,fourth   suffix';
echo preg_replace('#[,]{2,}#',',',preg_replace('#\s*,+\s*#',',',preg_replace('#\s+#s',' ',$str)));

which works well so far, but is it possible to do it in one replacement?

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

0

You can use:

[\h*([,\h])[,\h]*

See an online demo. Or alternatively:

\h*([,\h])(?1)*

See an online demo


  • \h* - 0+ (Greedy) horizontal-whitespace chars;
  • ([,\h]) - A 1st capture group to match a comma or horizontal-whitespace;
  • [,\h]* - Option 1: 0+ (Greedy) comma's or horizontal-whitespace chars;
  • (?1)* - Option 2: Recurse the 1st subpattern 0+ (Greedy) times.

Replace with the 1st capture group:

$str='first , second ,, third, ,fourth   suffix';
echo preg_replace('~\h*([,\h])[,\h]*~', '$1', $str);
echo preg_replace('~\h*([,\h])(?1)*~', '$1', $str);

Both print:

first,second,third,fourth suffix
over 4 years ago · Santiago Trujillo Relatório

0

You can use

preg_replace('~\s*(?:(,)\s*)+|(\s)+~', '$1$2', $str)

Merging the two alternatives into one results in

preg_replace('~\s*(?:([,\s])\s*)+~', '$1', $str)

See the regex demo and the PHP demo. Details:

  • \s*(?:(,)\s*)+ - zero or more whitespaces and then one or more occurrences of a comma (captured into Group 1 ($1)) and then zero or more whitespaces
  • | - or
  • (\s)+ - one or more whitespaces while capturing the last one into Group 2 ($2).

In the second regex, ([,\s]) captures a single comma or a whitespace character.

over 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