48

How can i check to see if a string only contains spaces?

tarnfeld
  • 25,992
  • 41
  • 111
  • 146
  • 4
    *Any* whitespace or just spaces? – William Brendel Feb 28 '10 at 21:37
  • possible duplicate of [How to check if there are only spaces in string in PHP?](http://stackoverflow.com/questions/1754393/how-to-check-if-there-are-only-spaces-in-string-in-php) – Mołot Mar 05 '14 at 10:11
  • 1
    `substr_count($str, ' ') === strlen($str)` – caw Jul 27 '16 at 01:10
  • @caw Best answer! The question asked for a way to detect if a string only contains **spaces**, most answers check if string only contain **whitespace character(s)**. – Leia Apr 02 '17 at 07:29

11 Answers11

65
if (strlen(trim($str)) == 0)

or if you don't want to include empty strings,

if (strlen($str) > 0 && strlen(trim($str)) == 0)
John Knoeller
  • 33,512
  • 4
  • 61
  • 92
  • I'd say `if (strlen(trim($str)) != strlen($str))` – Esteban Küber Feb 28 '10 at 21:39
  • 6
    @voyager: that would determine if the string had _any_ leading or trailing spaces, the question is does it have _only_ spaces. – John Knoeller Feb 28 '10 at 21:42
  • @voyager: What about `" hello world "`? In that case `strlen(trim($str))` is 11 and `strlen($str)` is 15, but the string is not *only* made up on spaces. – William Brendel Feb 28 '10 at 21:43
  • Point taken, but it would have to be `if ((strlen(trim($str)) != strlen($str)) && (strlen(trim($str)) == 0))` if you want to make sure that the string contains only whitespace, but **it's not** an *empty* string. – Esteban Küber Feb 28 '10 at 23:01
  • 3
    @voyger: that's still overkill. do `if (strlen($str)>0 && strlen(trim($str))==0)` if that's what you want. or use `!empty($str)`. why would you bother comparing it to the trimmed string? – mpen Mar 01 '10 at 01:21
  • This will be true if your string is empty, ex: $str == "", which you may not want – TravisO Mar 01 '10 at 04:08
  • @Travis0: That's already been pointed out, but tarnfeld apparently _did_ want to include empty strings. Still, since people keep wandering by to point it out again, I have edited the answer to make that clear. – John Knoeller Mar 01 '10 at 04:18
10

from: https://stackoverflow.com/a/2992388/160173

If you want to upvote, do it on the other answer, not this one!


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


as a function:

function stringIsNullOrWhitespace($text){
    return ctype_space($text) || $text === "" || $text === null;
}
Community
  • 1
  • 1
David Murdoch
  • 87,823
  • 39
  • 148
  • 191
6
echo preg_match('/^ *$/', $string)

Should work.

Enrico Carlesso
  • 6,818
  • 4
  • 34
  • 42
  • 4
    @stereofrog: I respectfully disagree. Regexes are overkill for something like this, that can be handled perfectly by built in functions. – Esteban Küber Mar 01 '10 at 16:30
  • 1
    That's true, and also maybe builtin functions like shown by John are faster than regexp. But I love regexp, and I use to do everything I can by them, even if not necessary at all. And anyway, I was sure that simplest solutions will have been posted, I just want people not to forget regexp :D – Enrico Carlesso Mar 01 '10 at 17:03
  • always returning 0 for -------- $str = "ffff gggg"; echo preg_match('/^ *$/',$str); – user889030 Jan 17 '17 at 17:21
4

Use a regular expression:

$result = preg_match('/^ *$/', $text);

If you want to test for any whitespace, not just spaces:

$result = preg_match('/^\s*$/', $text);
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

check if result of trim() is longer than 0

migajek
  • 8,524
  • 15
  • 77
  • 116
  • will not work ... run ... $str = 'ffffgggg'; if (strlen(trim($str)) > 0) echo 'space'; else echo 'nospace'; output is .. space – user889030 Jan 17 '17 at 17:12
3

I think using regexes is overkill, but here's another sol'n anyway:

preg_match('`^\s*$`', $str)
mpen
  • 272,448
  • 266
  • 850
  • 1,236
1

another way

preg_match("/^[[:blank:]]+$/",$str,$match);
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0
chop($str) === ''

This should be enough.

Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
0

If you are using Ck-editor then you should do this

if( strlen(trim($value,' ')) == 0 ){
    echo "White space found!"
}
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
-1

Another way, just for play

<?php  
function is_space_str($str) {
    for($i=0,$c=strlen($str);$i<$c;$i++) {
        switch (ord($str{$i})) {
            case 21: 
            case 9:
            case 10: 
            case 13: 
            case 0:
            case 11: 
            case 32: 
            break;
            default:
                return false;
        }   
    }   

    return true;
} 
Felipe Buccioni
  • 19,109
  • 2
  • 28
  • 28
-1

in my program it worked well like this,

$comment = $_POST['comment'];
$commentcheck = trim($comment);

if (empty($commentcheck)) 
{
    //instruction with $comment
}

trim() function just delete all spaces from $comment string. Then it check if it is empty. So in this if statement you have to show an error message then use else to code whatever you want to write.