这是我用于最少3个字符检查的简单代码。 如果查询是完全数字的,我想做一个例外(代码也有一个按案例ID搜索的选项,短于3个字符)。
<?php
if (strlen($_POST['Search'])>=3) {
include 'search.php';
} else {
$message = '3+ characters, please';
}
?>
感谢您的帮助:)
答案 0 :(得分:2)
使用此:
if (strlen($_POST['Search'])>=3 ||is_numeric($_POST['Search'])) {
//Do stuff
} else
//do other stuff
答案 1 :(得分:1)
如果我理解正确的话:
<?php
if (is_numeric($_POST['Search']) || strlen($_POST['Search'])>=3) {
?>
答案 2 :(得分:1)
你应该写这样的东西:
// 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) 但只有没有符号,十进制和指数部分。