PHP String Replace Space with Hyphen Example
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...
- 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