60

Possible Duplicate:
If string only contains spaces?

I do not want to change a string nor do I want to check if it contains white space. I want to check if the entire string is ONLY white space. What the best way to do that?

Community
  • 1
  • 1
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
  • @Dan McG I must suck at searching because I swear I searched for this before I posted it. – JD Isaacks Jun 07 '10 at 19:17
  • The question may be duplicate, but the answers are not. – webbiedave Jun 07 '10 at 19:19
  • @webbiedave The answers are similar. How many ways do you need to check for a space white space string? – Justin Johnson Jun 07 '10 at 19:33
  • 2
    @Justin Johnson: So if someone comes up with a better way to do it, it shouldn't be added or upvotable? – webbiedave Jun 07 '10 at 19:35
  • No, it should be posted on the first question. Just because a question has an answer, doesn't mean an alternate or better answer cannot be posted later. – Justin Johnson Jun 07 '10 at 19:38
  • @Justin Johnson: But with no rep incentive :( – webbiedave Jun 07 '10 at 19:40
  • @Justin Johnson: Maybe we should throw this on meta. I'd be curious to know the consensus on this. – webbiedave Jun 07 '10 at 19:41
  • My thinking is this: Duplicate questions decentralize responses and have negative effects on organization, which is why they have the option to be closed by the community (which I typically encourage in cases like this). However, I have seen questions that ask the same thing but illicit a completely different type of response where it made sense to keep both questions open (looking for link), but this is not one of those. Rep is nice, but to me it comes second to the organization and consistency of the resource that this community is building. – Justin Johnson Jun 07 '10 at 19:50
  • Also, the rep that you got for your answer wont go away if the question is closed (99% sure of that), so it doesn't really hurt those who have answered. – Justin Johnson Jun 07 '10 at 19:52
  • 1
    Rep only disappears on deleted questions. BTW, the question sited as an exact dup asks about detecting *spaces*, not *white space*. So the question is not an **exact** duplicate. – webbiedave Jun 07 '10 at 20:06
  • 1
    Plus, neither this question nor the other has an answer taking into account Unicode whitespace (preg_match('/^[\pZ\pC]$/u',$str)). Very lame indeed to close just because it seems that it might be a duplicate, while in reality there is just a vague analogy. – Unai Vivi Nov 15 '12 at 23:45

4 Answers4

130

This will be the fastest way:

$str = '      ';
if (ctype_space($str)) {

}

Returns false on empty string because empty is not white-space. If you need to include an empty string, you can add || $str == '' This will still result in faster execution than regex or trim.

ctype_space

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • 8
    Wow, I didn't imagine it could get much simpler than the trim, but I guess it can. +1 for a function I've never seen or used. – Matt Brunmeier Jun 07 '10 at 19:21
  • 2
    Check out: http://php.net/manual/en/book.ctype.php Check for alphanumeric character(s) ctype_alpha — Check for alphabetic character(s) ctype_cntrl — Check for control character(s) ctype_digit — Check for numeric character(s) ctype_graph — Check for any printable character(s) except space ctype_lower — Check for lowercase character(s) ctype_print — Check for printable character(s) ctype_punct — Check for any printable character which is not whitespace or an alphanumeric character ctype_space — Check for whitespace character(s) ctype_upper — Check for upperca... – Cas Bloem Sep 16 '14 at 08:26
  • "This will still result in faster execution than regex or trim." < True; http://ideone.com/t8ou8D – Cas Bloem Sep 16 '14 at 08:43
  • `ctype_space(" ") === false` – gondo Oct 31 '16 at 15:56
  • 1
    if empty check is needed, use `if (ctype_space($str . ' '))` – Divins Mathew Jan 24 '21 at 14:07
49

since trim returns a string with whitespace removed, use that to check

if (trim($str) == '')
{
 //string is only whitespace
}
MANCHUCK
  • 2,424
  • 1
  • 16
  • 22
13
if( trim($str) == "" )
    // the string is only whitespace

This should do the trick.

svens
  • 11,438
  • 6
  • 36
  • 55
5

preg_match('/^\s*$/',$string)

change * to + if empty is not allowed

Wrikken
  • 69,272
  • 8
  • 97
  • 136