• 1
name Punditsdkoslkdosdkoskdo

How to remove multiple whitespaces in PHP??

I'm getting $row['message'] from a MySQL database and I need to remove all whitespace like and so on.

$row['message'] = "This is   a Text 
 and so on 	     Text text.";

should be formatted to:

$row['message'] = 'This is a Text and so on Text text.';

I tried:

 $ro = preg_replace('/ss+/', ' ',$row['message']);
 echo $ro;

but it doesn't remove or , just single spaces. Can anyone tell me how to do that?

You need:

$ro = preg_replace('/s+/', ' ',$row['message']);

You are using ss+ which means whitespace(space, tab or newline) followed by one or more whitespace. Which effectively means replace two or more whitespace with a single space.

What you want is replace one or more whitespace with single whitespace, so you can use the pattern ss* or s+ (recommended)

  • 0
Reply Report