How to replace slash (/) with dash (-) in PHP

Replacing a slash with a dash can be especially useful for some activities related to implementing so-called SEO-friendly URLs, but it is not the only case.

If you need to do this, I have a good news – it’s very simple.

All you have to do is to use str_replace() function as it is presented below:

str_replace('/', '-', 'your/string')


Replace string with slashes to string with dashes.


$string = 'sports/blue/shoes';
$result = str_replace('/', '-', $string);

echo $result;


Output: sports-blue-shoes