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...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example