In this article, we will learn how to remove spaces from a string. You can replace the space with any other character using the string replacement function.


Example

Use the str_replace() function


index.php

<?php

// Declare a string 
$str = " Sample string with many spaces ";

// Using str_replace() function to remove spaces
$str = str_replace(' ', '', $str);

// echo 
echo $str;
?>


Output:

Samplestringwithmanyspaces