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

144
Visualizações
PHP str_replace array to remove spaces and reaplce with - and / with - also issue

Im having an issue with my following snippet, its more or less working.... what i need to do is replace spaces with - and also want to replace / with - but its adding double for / as its applying for spaces either side of it and not sure how to resolve.

PHP

$optimise_product_name_pre = "Simplified PHP Invoice / Billing System";
$optimise_product_name = str_replace(array(" ","/"), array("-",""), $optimise_product_name_pre);
return $optimise_product_name;

OUTPUT:

simplified-php-invoice--billing-system

EXPECTED:

simplified-php-invoice-billing-system

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

0

Sometimes string replacement cannot be done accurately using one str_replace() so all you have to do is split the process into 2 seperate str_replace() calls

$optimise_product_name_pre = "Simplified PHP Invoice / Billing System";

$optimise_product_name = str_replace(" / ", "-", $optimise_product_name_pre);
$optimise_product_name = str_replace(" ", "-", $optimise_product_name);

echo $optimise_product_name;

Result

Simplified-PHP-Invoice-Billing-System

If you really wanted the resulting string to be all lower case then add a strtolower() like this

$optimise_product_name_pre = "Simplified PHP Invoice / Billing System";

$optimise_product_name = str_replace(" / ", "-", $optimise_product_name_pre);
$optimise_product_name = str_replace(" ", "-", $optimise_product_name);

echo strtolower($optimise_product_name);

Result

simplified-php-invoice-billing-system

Of course replace my echo with the return you were using if this is indeed in a function.

On closer examination this also works

$optimise_product_name_pre = "Simplified PHP Invoice / Billing System";
$optimise_product_name = str_replace(array(" /"," "), array("","-"), $optimise_product_name_pre);

echo strtolower($optimise_product_name);
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