Joh*_*ler 51
if (strlen(trim($str)) == 0)
Run Code Online (Sandbox Code Playgroud)
或者如果你不想包含空字符串,
if (strlen($str) > 0 && strlen(trim($str)) == 0)
Run Code Online (Sandbox Code Playgroud)
来自:https : //stackoverflow.com/a/2992388/160173
这将是最快的方法:
$str = ' ';
if (ctype_space($str)) {
}
Run Code Online (Sandbox Code Playgroud)
返回false
空字符串,因为empty不是空格。如果您需要包含一个空字符串,则可以添加。|| $str == ''
这仍将比regex或trim更快地执行。
作为功能:
function stringIsNullOrWhitespace($text){
return ctype_space($text) || $text === "" || $text === null;
}
Run Code Online (Sandbox Code Playgroud)
echo preg_match('/^ *$/', $string)
Run Code Online (Sandbox Code Playgroud)
应该管用.
归档时间: |
|
查看次数: |
29236 次 |
最近记录: |