Php 清理和验证表单,但有一些字符异常

发布于 2024-12-04 05:35:47 字数 532 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰つ倒转 2024-12-11 05:35:47

您可以使用 preg_match 并应用正则表达式。

preg_match(字符串$pattern,字符串$TestString) 详细信息请参见此处

模式就是问题所在。您需要详细定义允许的内容。

例如,模式:

'~^\d{4}-\d{4}-\d{4,5}$~D'

是从开始 ^ 到结束 $ 的整个字符串。 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 detail

The pattern is the problem. You need to define in detail what is allowed.

For example, the pattern:

'~^\d{4}-\d{4}-\d{4,5}$~D'

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

热风软妹 2024-12-11 05:35:47

使用正则表达式 preg_match() 。或者,您也可以使用 sscanf() 来根据格式解析字符串的输入。

Use a regular expression with preg_match(). Alternatively, you can also use sscanf() to parse the input from a string according to a format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文