03-Apr-2023
.
Admin
Hi Dev,
This example is focused on how to string replace space with hyphen in php?. We will use replace space with hyphen in php - string. let’s discuss about php replace space with hyphen with code examples. We will use php replace space with hyphen.
In this example to Check if string replace space with hyphen in PHP. in this example, we will use to preg_replace() function. so Let's see following example with output.
Example : PHP String Replace Space with Hyphen
index.php
<?php
//creat string
$str = "Hello Developers Welcome to Nicesnippets.com";
//Replace Space with Hyphen
$newStr = preg_replace('#[ ]+#', '-', $str);
//print output
echo $newStr;
?>
Output:
Hello-Developers-Welcome-to-Nicesnippets.com
I hope it could help you...
#PHP