12

What is the difference between space and whitespace in PHP?

I saw on different places that to strip out all the spaces use str_replace() and use preg_replace() for whitespace.

Here is a reference: https://stackoverflow.com/a/2109339/4003463

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
Kayani
  • 369
  • 1
  • 4
  • 16

1 Answers1

15

In the added context to your question, a space is ascii 32 (that is, what you get when you press the spacebar).

A whitespace is any character that renders as a space, that is:

  • A space character
  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character
  • A form feed character
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    "Space" can be just `\u0020` as you say, but it can also be non-breaking space (`\u00A0`) or full-width space (`\u3000`), or one of other unicode codepoints that look like (and are used as) space. I agree that in context of `str_replace`, they probably just meant the "normal" space (`\u0020`), given that that function can't replace several different things in one run (unlike `preg_replace`). – Amadan Jun 21 '18 at 07:48
  • Is there any replacement for "white space", such as "any space"? – DawnSong Jul 27 '20 at 11:30
  • @DawnSong what do you mean by "any space"? How would it be different from the concept of whitespace? – Federico klez Culloca Jul 27 '20 at 12:33
  • @FedericoklezCulloca There is "white space", and no "black space". There might be some more inclusive word. – DawnSong Jul 28 '20 at 06:42
  • Don't charge words with meanings they do not have. "White space" is because we usually write on white paper and there is a literal "white space" where there are no words. It's not charged, it doesn't have a positive meaning as opposed to "black space", which is not even a thing. Let's not make this very important fight against racism ridiculous to the point where people just mock it. – Federico klez Culloca Jul 28 '20 at 06:53