Skip to content

All topics  /  PHP

How to String Replace Slash with Space in PHP?

PHP

Nov 18, 2022

Hi Dev,

In this tutorial, you will learn how to string replace slash with space in php?. Here you will learn replace slash with space in php - string. Here you will learn php replace space with slash with code examples. step by step explain php replace spaces with slash. Alright, let’s dive into the steps.

There are example to Check if string replace slash with space in PHP. in this example, we will use to str_replace() and preg_replacefunction to check string contains integer. so Let's the following example with output.

Example 1:


index.php

<?php    
    //str_replace multiple slash for multiple spaces
    $str = "php replace space  with   slash";
    $newstr = str_replace(" ", "/", $str);
    echo $newstr; 
?>

Output:

php/replace/space//with///slash

Example 2:

index.php

<?php    
    //use preg_replace to replace multiple spaces with a single slash
    $str = "php replace space  with   slash";
    $newstr = preg_replace('/[[:space:]]+/', '/', $str);
    echo $newstr;
?>

Output:

php/replace/space/with/slash 

I hope it could help you...