验证用户提交的内容是否至少有一个列入白名单的值且没有列入黑名单的值
我知道有很多这样的东西,但我正在寻找稍微不同的东西。
直接的 diff 对我来说不起作用。
我有一个允许标签的列表(数组),即
["engine", "chassis", "brakes", "suspension"]
我想检查用户输入的列表。 Diff 不起作用,因为用户可能无法输入所有选项,即 ["engine"]
但我仍然希望它通过。如果他们在列表中放入诸如 banana
之类的内容,我想要发生的事情就是失败。
I know there are a lot of these, but I'm looking for something slightly different.
A straight diff won't work for me.
I have a list (array) of allowed tags i.e.
["engine", "chassis", "brakes", "suspension"]
Which I want to check with the list the user has entered. Diff won't work, because the user may not enter all the options i.e. ["engine"]
but I still want this to pass. What I want to happen is fail if they put something like banana
in the list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
array_intersect()
,并且使用输入数组的大小检查结果数组的大小。如果结果较小,则输入包含不在“允许”数组中的一项或多项。如果它的大小相等,则其中的所有项目都在用户的输入中,因此您可以使用该数组做任何您想做的事情。You can use
array_intersect()
, and check the size of the resulting array with the size of the input array. If the result is smaller, then the input contains one or more items not in the 'allowed' array. If its size is equal, all items in it are in the user's input, so you can use the array do do whatever you want.使用 array_diff();
这将返回banana,因为它在$user 中,但不在$allowed 中。
Use array_diff();
This will return banana, as it is in $user, but not in $allowed.
array_diff()
:http://nl.php.net/array_diffarray_diff()
: http://nl.php.net/array_diff