I'm getting $row['message']
from a MySQL database and I need to remove all whitespace like \n
\t
and so on.
$row['message'] = "This is a Text \n and so on \t Text text.";
should be formatted to:
$row['message'] = 'This is a Text and so on Text text.';
I tried:
$ro = preg_replace('/\s\s+/', ' ',$row['message']);
echo $ro;
but it doesn't remove \n
or \t
, just single spaces. Can anyone tell me how to do that?
You need:
You are using
\s\s+
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
\s\s*
or\s+
(recommended)This replaces all tabs, all newlines and all combinations of multiple spaces, tabs and newlines with a single space.
This is what I would use:
a. Make sure to use double quotes, for example:
b. To remove extra whitespace, use:
It may not be the fastest solution, but I think it will require the least code, and it should work. I've never used mysql, though, so I may be wrong.
this will replace multiple tabs with a single tab
This outputs
Without preg_replace, with the help of loop.