Php 清理和验证表单,但有一些字符异常
我在 Php Sanitize 和 Validate Filters 中使用,但我在添加一些规则时遇到问题,我有一些 php 的基本知识,所以我认为这个问题对你来说很容易。
if ($_POST['ccp_n'] != "") {
$ccp = filter_var($_POST['ccp_n'], FILTER_SANITIZE_NUMBER_INT);
if (!filter_var($ccp, FILTER_VALIDATE_INT)) {
$errors .= 'Insert a valid code.<br/>';
}
} else {
$errors .= 'Insert a code.<br/>';
}
我需要添加最小和最大字符数 (14-15),并且我想接受此字符( - 或空格)。确切的序列是 0000-0000-0000 (最后四位数字也可以是 5
谢谢
I'm using in Php Sanitize and Validate Filters but I have problems to add some rules, I have some basic knowledge of php so I think this question is easy for you.
if ($_POST['ccp_n'] != "") {
$ccp = filter_var($_POST['ccp_n'], FILTER_SANITIZE_NUMBER_INT);
if (!filter_var($ccp, FILTER_VALIDATE_INT)) {
$errors .= 'Insert a valid code.<br/>';
}
} else {
$errors .= 'Insert a code.<br/>';
}
I need to add a minimum and maximum number of characters (14-15) and I want to accept this characters ( - or space ) .The exact sequence is 0000-0000-0000 (the last four digits could be 5 too
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
preg_match
并应用正则表达式。preg_match(字符串$pattern,字符串$TestString)
详细信息请参见此处模式就是问题所在。您需要详细定义允许的内容。
例如,模式:
是从开始
^
到结束$
的整个字符串。 4 位数字,连字符,4 位数字,连字符,4 到 5 位数字。参见Regexr
更新:
我添加了 < code>D 修饰符到末尾,否则
$
不仅匹配字符串的末尾,而且还匹配换行符之前作为字符串中的最后一个字符。 请参阅此处了解 php 修饰符的详细信息You can use
preg_match
and apply a regular expression.preg_match ( string $pattern , string $TestString)
See here in detailThe pattern is the problem. You need to define in detail what is allowed.
For example, the pattern:
would be the whole string from start
^
to the end$
. 4 digits, hyphen, 4 digits, hyphen, 4 to 5 digits.See it here on Regexr
Update:
I added the
D
modifier to the end, otherwise the$
not only match to the end of the string, but also before a newline as last character in the string. See here for php modifiers in detail使用正则表达式
preg_match()
。或者,您也可以使用sscanf()
来根据格式解析字符串的输入。Use a regular expression with
preg_match()
. Alternatively, you can also usesscanf()
to parse the input from a string according to a format.