776

How can I strip / remove all spaces of a string in PHP?

I have a string like $string = "this is my string";

The output should be "thisismystring"

How can I do that?

6
  • See also: stackoverflow.com/questions/1279774/…
    – Mark Byers
    Jan 21, 2010 at 13:11
  • 2
    Technically, this is a slightly different question to the duplicate question. Sometimes you might want to strips spaces without tabs.
    – haz
    Dec 7, 2017 at 22:12
  • 1
    Here is the shortest way of doing this, in case you ever need to win at Code Golf: strtr($string,[' '=>'']);
    – haz
    Dec 7, 2017 at 22:13
  • 1
    To only strip from the beginning and end, use the trim functions: php.net/trim
    – Tim Visée
    May 29, 2019 at 19:58
  • The answers that said preg_replace('/\s+/', '', $string) are wrong or rather incomplete. The correct and complete answer is: preg_replace('/\s+/u', '', $string). Because other solutions   (or Alt + 255) cannot be removed from the string, but my solution even   also deletes.
    – enaeim
    Jul 25, 2023 at 7:49

4 Answers 4

1736

Do you just mean spaces or all whitespace?

For just spaces, use str_replace:

$string = str_replace(' ', '', $string);

For all whitespace (including tabs and line ends), use preg_replace:

$string = preg_replace('/\s+/', '', $string);

(From here).

10
  • 51
    1st, you don't need the +. $string = preg_replace('/\s/', '', $string); should work fine. The only thing this doesn't work on for me is non-breaking spaces. I had to use this: $string = preg_replace('~\x{00a0}~','',$string); to remove them. Thanks to this stackoverflow answer: stackoverflow.com/a/12838189/631764 Jul 19, 2013 at 0:04
  • 47
    Newbie question out of interest: What is the difference between space and whitespace? Isn't it the same?
    – Avatar
    Sep 12, 2013 at 10:50
  • 57
    @EchtEinfachTV space is the regular space character between words, whitespace is any kind of space in text: regular space, new line, tab, etc
    – juuga
    Sep 13, 2013 at 8:37
  • 3
      is HTML string, you need to remove it on your own. The "whitespace" characters are HT (9), LF (10), FF (12), CR (13), and space (32). However, if locale-specific matching is happening, characters with code points in the range 128-255 may also be considered as whitespace characters, for instance, NBSP (A0).
    – stamster
    Jan 20, 2017 at 13:59
  • 1
    I always recommend \s+ when it is possible that multiple spaces could occur. This way longer and fewer matches are made with fewer total replacement -- eventuating in the same result. May 3, 2021 at 11:41
70

If you want to remove all whitespace:

$str = preg_replace('/\s+/', '', $str);

See the 5th example on the preg_replace documentation. (Note I originally copied that here.)

Edit: commenters pointed out, and are correct, that str_replace is better than preg_replace if you really just want to remove the space character. The reason to use preg_replace would be to remove all whitespace (including tabs, etc.).

8
  • 8
    »If you don't need fancy replacing rules (like regular expressions), you should always use this function [str_replace] instead of ereg_replace() or preg_replace()
    – Joey
    Jan 21, 2010 at 13:06
  • 2
    I would not suggest using regular expressions for simple space removal. str_replace for just spaces, preg_replace for all whitespace. Jan 21, 2010 at 13:08
  • 1
    @Johannes & MasterPeter: Agreed, but the OP left a comment on another answer that he wants to strip white space.
    – GreenMatt
    Jan 21, 2010 at 13:08
  • 1
    Hopelessly broken this one, will remove 2 or more whitespaces, not single, as requested in the example.
    – user163365
    Jan 21, 2010 at 13:20
  • 2
    No, it removes one or more (plus sign means "or more", not "and more").
    – Brilliand
    Oct 5, 2012 at 21:32
52

If you know the white space is only due to spaces, you can use:

$string = str_replace(' ','',$string); 

But if it could be due to space, tab...you can use:

$string = preg_replace('/\s+/','',$string);
0
18

str_replace will do the trick thusly

$new_str = str_replace(' ', '', $old_str);
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.