Remove multiple whitespaces
Solution 1
You need:
$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)
Solution 2
<?php
$str = "This is a string with
spaces, tabs and newlines present";
$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);
echo $str;
echo "\n---\n";
echo "$stripped";
?>
This outputs
This is a string with
spaces, tabs and newlines present
---
This is a string with spaces, tabs and newlines present
Solution 3
preg_replace('/[\s]+/mu', ' ', $var);
\s
already contains tabs and new lines, so this above regex appears to be sufficient.
Solution 4
simplified to one function:
function removeWhiteSpace($text)
{
$text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);
$text = preg_replace('/([\s])\1+/', ' ', $text);
$text = trim($text);
return $text;
}
based on Danuel O'Neal answer.
Solution 5
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);
Related videos on Youtube
creativz
Updated on March 08, 2021Comments
-
creativz about 3 years
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?-
Mark Lalor over 13 yearsThe newline and tab characters are in single quotes, so you want them literal?
-
Buttle Butkus over 10 yearsI fixed the quoting of the code sectin with the \n and \t by changing it to double-quotes.
-
-
nickf about 14 yearshis method is better than this: why would you replace one space with one space?
-
codaddict about 14 yearsHe 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 about 14 years+1, True. For a string with plenty of single spaces (which usually is the case) it is inefficient to replace a space with space.
-
Dziamid almost 12 yearsThis is the one that worked for me the best. Also, I would add trim to erase whitespace in the beginning and end of string
-
Mansoorkhan Cherupuzha over 10 years@codaddict, how we can keep \n and remove all other multiple space and tabs from the string? please help me
-
Isius over 10 yearsCan you be more specific why "\s+" is recommended?
-
Yaroslav over 10 yearsNote that in PHP
\s
not including "vertical tab"chr(11)
. To include it too you need to usespace
character class:[[:space:]]+
php.net/manual/en/regexp.reference.character-classes.php -
bikey77 over 10 yearsYou are a true lifesaver. I was about to jump out if the window over this.
-
Timo Huovinen over 10 yearskeep in mind that this will also replace newlines
\r\n
with a single space ` ` -
e-info128 about 10 years$row['message'] = 18; // $ro is null.
-
Joseph Cheek about 10 years@coaddict: to test your hypothesis, i wrote a quick script to run through 1000 of each replacement and check the timing of each. For the string '+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 Feb 24 \'10 at 13:32', one thousand \s+ preg_replace() calls took 0.010547876358032 seconds, and one thousand (?:\s\s+|\n|\t) preg_replace() calls took 0.013049125671387, making it almost 30% slower.
-
Marcello Mönkemeyer over 9 years@WHK
preg_replace()
requires the third parameter to be either a string or an array full of strings. Otherwise, it will returnnull
, meaning an error occured.$row['message'] = 18; // $ro = null
while$row['message'] = '18'; // $ro = '18'
and$row['message'] = (string) 18; // $ro = '18'
-
Balázs Varga over 9 years@Dziamid You can do it with trim(preg_replace(...))
-
Mai about 9 yearsI want to remove spaces at the beginning or at the end of the text. If the string is
' Text with a lot of spaces '
, it returns' Text with a lot of spaces '
. I expect'Text with a lot of spaces'
. -
Lukas Liesis almost 9 yearsstatic function remove_whitespace is for what reason? You define but never use it.
-
thomasrutter about 8 yearsSquare brackets aren't needed here because there's only one thing inside them. The
/m
wont have an effect as there are no^
or$
anchors and the/u
won't have any effect except to slow it down slightly and die if the input string is not valid UTF-8 (it doesn't affect what\s
matches, but it would affect\pZ
). -
thomasrutter about 8 yearsThese each have their use but none of these would achieve what the question asks for which is to replace multiple consecutive whitespace with just one. Your "remove_doublewhitespace" would only replace multiple of the same whitespace character, so it would replace "\n\n\n" with a ' ', but it would not do anything to " \r\n"
-
thomasrutter about 8 yearsYou may want to add "\r" to that last example as some computers do use a single "\r" on its own (Apple Mac?)
-
TARKUS almost 8 yearsI tried using this method, which works, except that instead of " " I get
. Normally, this wouldn't matter, but I'm exporting the result text to an Excel spreadsheet, and those
are showing up in the spreadsheet cell. It's bad because the spreadsheet cell text is going to be used to print mailing labels. -
TARKUS almost 8 yearsHere's a method that will also remove
preg_replace(array('/\s{2,}| /', '/[\t\n]/'), ' ', $str)` -
yussan over 7 yearsyeah, you saved my life, this is worked as my exspectation
-
eawedat over 7 years@codaddict , nice job, thank you, with keeping this regex pattern, is there a way to get rid of all spaces at the right & and the left of the string? for example, " a b c ", would be "a b c", I know we could use trim($ro), but it would be nice to have it in regex
-
jalmatari over 4 yearsIt works with me even in mariaDB in this query:
SELECT search_able, REGEXP_REPLACE (search_able,"\\s+",' ') FROM book where id =260
So Thanks a lot -
spekulatius over 4 yearsNeat, still helpful
-
Toto about 3 years
\t
&\n
are already included in\s
so your regex is exactly the same than\s\s+
that is better written\s{2,}
just like @Alex Polo answer -
Toto about 3 yearsThis will remove all the spaces even those that souldn't be removed. Moreover, it doesn't remove tabulation
\t
and linebreak. -
Lulucmy almost 2 yearsWell I'd never have thought about this, but 12 years later it still works well!