搜索过滤器:最少字符数


Search filter : minimum characters

这是我用于至少 3 个字符检查的简单代码。如果查询是全数字的,我想例外(代码还有一个选项,可以按短于 3 个字符的案例 ID 进行搜索)。

<?php
if (strlen($_POST['Search'])>=3) {
include 'search.php';
} else {
$message = '3+ characters, please';
}
?>

感谢您的帮助:)

使用这个:

if (strlen($_POST['Search'])>=3 ||is_numeric($_POST['Search'])) {
  //Do stuff
} else
//do other stuff

你应该写这样的东西:

// make sure no notices are emitted if the input is not as expected
$search = isset($_POST['Search']) ? $_POST['Search'] : null;
if (!ctype_digit($search)) {
    // it's all digits
}
else if (strlen($search) < 3) {
    // error: less than three characters
}
else {
    // default case
}

随意合并分支是默认情况,"所有数字"情况应转发到相同的代码。

重要提示使用 ctype_digit 检查输入是否全是数字。 is_numeric 还将返回其他类型的输入的true

数字字符串由可选符号、任意位数、 可选小数部分和可选指数部分。 因此 +0123.45e6 是一个有效的数值。也允许使用十六进制表示法 (0xFF) 但只有没有符号、小数和指数部分。

如果我

理解正确的话:

<?php
if (is_numeric($_POST['Search']) || strlen($_POST['Search'])>=3) {
?>