Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!