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

148
Visualizações
Replacing a character in $args

I want to replace each + with (space) in $args.

I know the solution of

if ($args ~ ^(.*)\+(.*)$) {
  return 301 "$uri?$1 $2";
}

However that's not ideal because there's a redirect for each +. Is there a better solution?

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

0

You could use more than one regular expression. Each regular expression tests the $args parameter for one, two, three, etc. + characters (evaluated in reverse order) which will minimise the recursion.

A compact solution uses a map to define the regular expressions.

For example:

map $args $spaces {
    default 0;
    ~^(.*)\+(.*)\+(.*)\+(.*)\+(.*)$  "$1 $2 $3 $4 $5";
    ~^(.*)\+(.*)\+(.*)\+(.*)$        "$1 $2 $3 $4";
    ~^(.*)\+(.*)\+(.*)$              "$1 $2 $3";
    ~^(.*)\+(.*)$                    "$1 $2";
}
server {
    ...
    if ($spaces) { return 301 "$uri?$spaces"; }
    ...
}

You can use more or less regular expressions to optimise the solution. The above will generate one redirection for up to four + characters, and two redirections for between five and eight + characters, and so on.

See this document for details.

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