views:

892

answers:

7

I'm getting $row['message'] from my mysql db and I need to remove all whitespaces like \n \t and so on.

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

should be formated 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 dosn't removes \n or \t, just single spaces. Can anyone tell me how to do that? Thanks!

+11  A: 

Try:

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

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)

codaddict
his method is better than this: why would you replace one space with one space?
nickf
He also wants \n and \t to be replaced with space. Now his pattern does not match these, say for $x = "does\nthis\twork"; The OP wants all whitespace to be replaced with a single space.
codaddict
A: 
$str='This is   a Text \n and so on Text text.';
print preg_replace("/[[:blank:]]+/"," ",$str);
ghostdog74
+2  A: 

I can't replicate the problem here:

$x = "this    \n \t\t \n    works.";
var_dump(preg_replace('/\s\s+/', ' ', $x));
// string(11) "this works."

I'm not sure if it was just a transcription error or not, but in your example, you're using a single-quoted string. \n and \t are only treated as new-line and tab if you've got a double quoted string. That is:

'\n\t' != "\n\t"

Edit: as Codaddict pointed out, \s\s+ won't replace a single tab character. I still don't think using \s+ is an efficient solution though, so how about this instead:

preg_replace('/(?:\s\s+|\n|\t)/', ' ', $x);
nickf
+1, True. For a string with plenty of single spaces (which usually is the case) it is inefficient to replace a space with space.
codaddict
+2  A: 
<?php

$str = "This is  a string       with
spaces, tabs and newlines present";

$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);

print "$str\n";
print "$stripped\n";

This outputs

This is  a string   with
spaces, tabs and newlines present
This is a string with spaces, tabs and newlines present
Cez
A: 

All you need is to run it as follows:

echo preg_replace('/\s{2,}/', ' ', "This is   a Text \n and so on \t     Text text."); // This is a Text and so on Text text.
Alex Polo
A: 
preg_replace('/(\s\s+|\t|\n)/', ' ', $row['message']);

This replaces all tabs, all newlines and all combinations of multiple spaces, tabs and newlines with a single space.

middus
A: 

This is what I would use:

a. Make sure to use double quotes, for example:

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

b. To remove extra whitespace, use:

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

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.

matsolof