Sometimes you would need to delete consecutive white spaces. You can do it like this:
$str = "My name is faisal";
$str = preg_replace('/\s\s+/', ' ', $str);
Output:
My name is faisal
**N.B:** [str_replace][1] is better than [preg_replace][2] if you really just want to remove the space character. The reason to use [preg_replace][2] would be to remove all whitespace (including tabs, newlines, etc.)
[1]: http://php.net/manual/en/function.str-replace.php
[2]: http://php.net/manual/en/function.preg-replace.php
Sometimes you would need to delete consecutive white spaces. You can do it like this:
$str = "My name is";
$str = preg_replace('/\s\s+/', ' ', $str);
Output:
My name is