Check if string is just white space? [duplicate]
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?
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
since trim returns a string with whitespace removed, use that to check
if (trim($str) == '')
{
//string is only whitespace
}
if( trim($str) == "" )
// the string is only whitespace
This should do the trick.
preg_match('/^\s*$/',$string)
change * to + if empty is not allowed