如果下面的变量不包含字符,只有任意数量的空格,我想使用 header("Location: URL"); exit();
将用户重定向到 URL。
我该怎么做?
$comment = mysql_real_escape_string($_POST['comment']);
这里有
2种方法:
if (trim($var, ' ') == '') {
// $var consists of only spaces
}
// or
if (str_replace(' ', '', $var) == '') {
// $var consists of only spaces
}
我会使用 PCRE 的 ''s 标识符,这也将捕获选项卡等:
if (preg_match('/^'s+$/', $comment) { ... }
[编辑] 或者,如果您还想捕获完全空的字符串:
if (preg_match('/^'s*$/', $comment) { ... }
if(strlen(trim($comment)) == 0) header('Location: URL');
!trim( $_POST['comment'] ) && header( "Location: URL" );
if(str_replace(" ", "", $comment) == "")
{
header("Location: URL");
exit();
}