762

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
streetparade
  • 32,000
  • 37
  • 101
  • 123
  • See also: http://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php – Mark Byers Jan 21 '10 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 07 '17 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 07 '17 at 22:13
  • 1
    To only strip from the beginning and end, use the `trim` functions: https://php.net/trim – Tim Visée May 29 '19 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 '23 at 07:49
  • Str::remove([ '', ' '], $string); – Caro Pérez Sep 01 '23 at 19:30

4 Answers4

1712

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).

PaulH
  • 2,918
  • 2
  • 15
  • 31
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 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: http://stackoverflow.com/a/12838189/631764 – Buttle Butkus Jul 19 '13 at 00:04
  • 46
    Newbie question out of interest: What is the difference between `space` and `whitespace`? Isn't it the same? – Avatar Sep 12 '13 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 '13 at 08:37
  • You need @ButtleButkus's answer for cleaning up WYSIWYG content, which includes non-breaking spaces. – fideloper Jul 22 '14 at 19:49
  • If you're seeing strange new characters after implementing this solution, you may want to use the solution found here: http://stackoverflow.com/q/1176904/80857 – Keyslinger Oct 14 '15 at 20:22
  • This doesn't seem to remove ` ` characters – Jack Dec 01 '16 at 16:45
  • 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 '17 at 13:59
  • The trim should do the right and left trim of spaces. Most of the time you want remove more than one space. Therefore it is better use a combination of trim and regex. Ex: $txt = ' Hello World! '; $txt = trim(preg_replace('/\s{2}+/','',$txt)); echo '
    '.$txt.'
    '; I am really surprised, that this being a common occurrence PHP API is not providing an solution for it.
    – Shailesh Kumar Dayananda Mar 20 '17 at 06:08
  • 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. – mickmackusa May 03 '21 at 11:41
  • @ButtleButkus The **+** is required to handle multiple spaces. For instance 3 spaces in a row !!!! – Rohit Gupta May 05 '22 at 23:07
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.).

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
Arkaaito
  • 7,347
  • 3
  • 39
  • 56
  • 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 '10 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. – Peter Perháč Jan 21 '10 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 '10 at 13:08
  • Good point, Johannes. I went for preg_replace() because I misread the question, and thought what he meant was whitespace generally (not necessarily the specific space character). – Arkaaito Jan 21 '10 at 13:08
  • 1
    Hopelessly broken this one, will remove 2 or more whitespaces, not single, as requested in the example. – user163365 Jan 21 '10 at 13:20
  • 2
    No, it removes one or more (plus sign means "or more", not "and more"). – Brilliand Oct 05 '12 at 21:32
  • Worked well for me. – Joseph Mar 31 '22 at 04:03
  • This looks like a duplicate of [an earlier answer](https://stackoverflow.com/a/2109339/450127) – Ian Dunn May 09 '22 at 20:15
49

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);
codaddict
  • 445,704
  • 82
  • 492
  • 529
18

str_replace will do the trick thusly

$new_str = str_replace(' ', '', $old_str);
David Heggie
  • 2,868
  • 1
  • 24
  • 21