web*_*ave 109
这将是最快的方式:
$str = ' ';
if (ctype_space($str)) {
}
Run Code Online (Sandbox Code Playgroud)
在空字符串上返回false,因为empty 不是空格.如果你需要包含一个空字符串,你可以添加|| $str == ''这仍然会比正则表达式或修剪更快地执行.
MAN*_*UCK 42
因为trim返回一个删除了空格的字符串,所以用它来检查
if (trim($str) == '')
{
//string is only whitespace
}
Run Code Online (Sandbox Code Playgroud)
sve*_*ens 11
if( trim($str) == "" )
// the string is only whitespace
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题.
preg_match('/^\s*$/',$string)
如果不允许为空,请将 * 更改为 +