I have a string like "Tom Jerry ". I want to trim the extra space from middle of the string(there is 2 white space between first name and last name, I want only one white space). I.e. desirable output is "Tom Jerry". How do I do that?
Asked
Active
Viewed 341 times
1 Answers
1
Try this
$str = "Tom Jerry";
$cleanStr = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $str)));
echo $cleanStr;

shubham715
- 3,324
- 1
- 17
- 27