In this blog, I am going to show you how to replace spaces from a string. We are going to use two functions of PHP to replace spaces str_replace() and preg_replace().
Example 1: Using str_replace() function
$string = "This is thecodingdev.com";
$string = str_replace(' ', '', $string);
print_r($string);
//Thisisthecodingdev.com
Example 2: Using preg_replace() function
$string = "This is thecodingdev.com";
$string = preg_replace('/\s+/', '', $string);
print_r($string);
//Thisisthecodingdev.com
I hope this will be helpful in your coding, let me know in comments if you have any doubts.
0 Comments