Remove multiple whitespaces

256,527

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);
Share:
256,527

Related videos on Youtube

creativz
Author by

creativz

Updated on March 08, 2021

Comments

  • creativz
    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
      Mark Lalor over 13 years
      The newline and tab characters are in single quotes, so you want them literal?
    • Buttle Butkus
      Buttle Butkus over 10 years
      I fixed the quoting of the code sectin with the \n and \t by changing it to double-quotes.
  • nickf
    nickf about 14 years
    his method is better than this: why would you replace one space with one space?
  • codaddict
    codaddict about 14 years
    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
    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
    Dziamid almost 12 years
    This 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
    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
    Isius over 10 years
    Can you be more specific why "\s+" is recommended?
  • Yaroslav
    Yaroslav over 10 years
    Note that in PHP \s not including "vertical tab" chr(11). To include it too you need to use space character class: [[:space:]]+ php.net/manual/en/regexp.reference.character-classes.php
  • bikey77
    bikey77 over 10 years
    You are a true lifesaver. I was about to jump out if the window over this.
  • Timo Huovinen
    Timo Huovinen over 10 years
    keep in mind that this will also replace newlines \r\n with a single space ` `
  • e-info128
    e-info128 about 10 years
    $row['message'] = 18; // $ro is null.
  • Joseph Cheek
    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
    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 return null, 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
    Balázs Varga over 9 years
    @Dziamid You can do it with trim(preg_replace(...))
  • Mai
    Mai about 9 years
    I 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
    Lukas Liesis almost 9 years
    static function remove_whitespace is for what reason? You define but never use it.
  • thomasrutter
    thomasrutter about 8 years
    Square 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
    thomasrutter about 8 years
    These 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
    thomasrutter about 8 years
    You may want to add "\r" to that last example as some computers do use a single "\r" on its own (Apple Mac?)
  • TARKUS
    TARKUS almost 8 years
    I tried using this method, which works, except that instead of " " I get &nbsp;. Normally, this wouldn't matter, but I'm exporting the result text to an Excel spreadsheet, and those &nbsp; 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
    TARKUS almost 8 years
    Here's a method that will also remove &nbsp; preg_replace(array('/\s{2,}|&nbsp;/', '/[\t\n]/'), ' ', $str)`
  • yussan
    yussan over 7 years
    yeah, you saved my life, this is worked as my exspectation
  • eawedat
    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
    jalmatari over 4 years
    It 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
    spekulatius over 4 years
    Neat, still helpful
  • Toto
    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
    Toto about 3 years
    This will remove all the spaces even those that souldn't be removed. Moreover, it doesn't remove tabulation \t and linebreak.
  • Lulucmy
    Lulucmy almost 2 years
    Well I'd never have thought about this, but 12 years later it still works well!