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
  • 27
  • 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
  • 451,927
  • 24
  • 239
  • 318
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,173
  • 68
  • 59
  • 86