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
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
echowith thereturnyou 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);