PHP 清理/验证整数数组
我有以下数组,想知道验证和清理该数组以确保仅允许整数的最佳方法是什么?
if(is_array($_POST['taxonomy'])) {
$term_ids = array_map('esc_attr', $_POST['taxonomy']);
}
打印时看起来像这样:
Array
(
[0] => 13
[1] => 12
)
我知道 esc_attr 不是很安全,所以想要一些更加强的东西。
任何帮助都会很棒。
干杯,
戴夫
I have the following array and would like to know what the best way would be of validating and santizing this array to make sure only integers are allowed?
if(is_array($_POST['taxonomy'])) {
$term_ids = array_map('esc_attr', $_POST['taxonomy']);
}
Which looks like this when printed:
Array
(
[0] => 13
[1] => 12
)
I know the esc_attr isn't very secure so would like something a bit more beefed up.
Any help would be great.
Cheers,
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于它是
$_POST
数据,因此您需要检查ctype_digit
(即仅包含数字的字符串):请注意,这只是丢弃非数字值。
Since it's
$_POST
data, you'll want to check forctype_digit
(i.e. a string containing only digits):Note that this simply discards non-numeric values.
另一种方法是使用 phps filter 函数:
An alternative would be using phps filter functions:
应该做到这一点。注意:这是卫生。更多:http://php.net/manual/en/function.intval.php
Should do the trick. NOTE: this is sanitation. More: http://php.net/manual/en/function.intval.php
如果您正在寻找单行代码来检查指定条件,您可以使用:
假设您的
$_POST['taxonomy']
可以包含@deceze 建议的数字字符串,或者只是简单:如果您确定
$_POST['taxonomy']
值实际上应该是整数。Arr::check
方法是此 php 数组库 的一部分其中包含各种方法来帮助您处理不同类型的数组。If you are looking for one-liner to check against specified condition you can use:
assuming your
$_POST['taxonomy']
can contain numeric strings as @deceze suggested, or just plain:if you are sure that
$_POST['taxonomy']
values should be in fact integers.Arr::check
method is a part of this php array library which contains various methods to help you deal with different types of arrays.