-1

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?

steniya
  • 37
  • 1
  • 1
  • 10

1 Answers1

1

Try this

$str = "Tom  Jerry";
$cleanStr = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $str)));
echo $cleanStr;
shubham715
  • 3,324
  • 1
  • 17
  • 27