How to String Replace Dash with Space in PHP?

Nov 14, 2022

.

Admin

How to String Replace Dash with Space in PHP?

Hi Dev,

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

There are example to Check if string replace dash 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 dash for multiple spaces

$str = "php replace space with dash";

$newstr = str_replace(" ", "-", $str);

echo $newstr;

?>

Output:

php-replace-space--with---dash

Example 2:

index.php

<?php

//use preg_replace to replace multiple spaces with a single dash

$str = "php replace space with dash";

$newstr = preg_replace('/[[:space:]]+/', '-', $str);

echo $newstr;

?>

Output:

php-replace-space-with-dash

I hope it could help you...

#PHP