使用PHP过滤器函数像filter_var_array()有一种方法来检查输入字符串的长度是否小于某个值 [英] Using PHP filter functions like filter_var_array() is there a way to check if length of an input string is less than some value
问题描述
我一直在玩PHP过滤器库。我喜欢它,但我无法执行一个简单的过滤器功能。我本质上想要使输入数组中的那些值是无效的,这些值是字符串并且长于某个值。
是否有办法做到这样,
$ data = array('input_string_array'=> array 'aaa','abaa','abaca'));
$ args = array(
'component'=> array('filter'=> FILTER_DEFAULT,
'flags'=> FILTER_REQUIRE_ARRAY,
'options'=> ; array('min_length'=> 1,'max_length'=> 10)
)
);
var_dump(filter_var_array($ data,$ args));
我试过这个和它给我的错误。因为大概没有可用的min_length / max_length选项。但是如何实现呢?还有一个地方,其中提到所有这样的选项,如max_range,min_range,regexp。
还有一个疑问,在过滤器,在FILTER_CALLBACK过滤器。我想知道是否有一种方法来传递一个参数而不是数据到被调用的函数?类似这样的
echo filter_var($ string,FILTER_CALLBACK,array(options=> array(lengthChecker, 5)));非常感谢您的帮助。 >解决方案除非有更好的,更直接的过滤器+选项,你可以使用 FILTER_VALIDATE_REGEXP
$ data = array('input_string_array'=& ,'abaa','abaca'));
$ args = array(
'input_string_array'=> array(
'filter'=> FILTER_VALIDATE_REGEXP,
'flags'=> FILTER_REQUIRE_ARRAY | FILTER_NULL_ON_FAILURE,
'options'=> array('regexp'=>'/ ^。{1,3} $ /')
)
);
var_dump(filter_var_array($ data,$ args));
print
array(1){
[input_string_array] =>
array(4){
[0] =>
NULL
[1] =>
string(3)aaa
[2] =>
NULL
[3] =>
NULL
}
}
元素可以使用eg array_filter()。
I have been toying with PHP filter library. I liked it but I am unable to perform a simple filter function. I essentially want to invalidate those values in my input array that are strings and which are longer than certain value.
Is there a way to do this like,
$data = array('input_string_array' => array('aaa', 'abaa', 'abaca'));
$args = array(
'component' => array('filter' => FILTER_DEFAULT,
'flags' => FILTER_REQUIRE_ARRAY,
'options' => array('min_length' => 1, 'max_length' => 10)
)
);
var_dump(filter_var_array($data, $args));
I tried this and its giving me error. because presumably there is no min_length/max_length option available. But then how to implement this? Also is there a place where it is mentioned about all such options like max_range, min_range, regexp.
Also I had another doubt, in filters, in FILTER_CALLBACK filter. I wanted to know if there is a way to pass a parameter other than the data to the called function? something like this,
echo filter_var($string, FILTER_CALLBACK, array("options"=> array("lengthChecker", "5")));
Thanks a lot for the help.
解决方案 Unless there's a better, more direct filter+option you can use FILTER_VALIDATE_REGEXP
$data = array('input_string_array' => array('', 'aaa', 'abaa', 'abaca'));
$args = array(
'input_string_array' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'flags' => FILTER_REQUIRE_ARRAY|FILTER_NULL_ON_FAILURE,
'options' => array('regexp'=>'/^.{1,3}$/')
)
);
var_dump(filter_var_array($data, $args));
prints
array(1) {
["input_string_array"]=>
array(4) {
[0]=>
NULL
[1]=>
string(3) "aaa"
[2]=>
NULL
[3]=>
NULL
}
}
To get rid of the NULL elements you can use e.g. array_filter().
这篇关于使用PHP过滤器函数像filter_var_array()有一种方法来检查输入字符串的长度是否小于某个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!