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