1

I want to remove only multiple white space in a string.

Example:

I am Mert Inal , so[multible white space here]I[multible white space here]can do that

It's must be:

I am Mert Inal soIcan do that

bymerdo
  • 77
  • 1
  • 5

2 Answers2

2

Try using preg_replace with the pattern \s{2,}, and replace with empty string:

$input = "I am Mert Inal , so    I    can do that";
$output = preg_replace("/\s{2,}/", "", $input);
echo $output;

This outputs:

I am Mert Inal , soIcan do that
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

you can try

$string = "I am Mert Inal , so  I   ";

echo preg_replace('/\s+/', ' ',$string);

Output:

I am Mert Inal , so I .

Note:
/s is also applicable for \n\r\t.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92