有没有一种方法来发送参数到一个回调函数不首先创造我自己的功能? [英] Is there a way to send parameters into a callback function without creating my own function first?
问题描述
我有值的数组,我想通过用htmlspecialchars但是有一个参数像这样运行:
I have an array of values that I would like to run through htmlspecialchars but with an argument such as this:
$param = htmlspecialchars($param, ENT_QUOTES);
问题是,我有值的数组,我想上运行的htmlspecialchars:
The problem is, I have an array of values that I want to run htmlspecialchars on:
$array = array_map('htmlspecialchars', $array);
和我想知道是否有到ENT_QUOTES通入array_map回调的方式?
and I would like to know if there is a way to pass ENT_QUOTES into the array_map callback?
我可以随时使用自己的函数使用的htmlspecialchars,但如果有办法已经做到这一点就好了。
I can always use my own function that uses htmlspecialchars, but it would be nice if there was a way to do this already.
下面的答案后,这里是我的最终结果是:
After the answer below, here is my end result:
$array = array_map('htmlspecialchars', $array, array_fill(0, count($array), ENT_QUOTES));
它只是填充有尽可能多的数值数组作为$阵列,它的充满ENT_QUOTE。
Which simply fills an array with as many values as $array has and it's filled with ENT_QUOTE.
推荐答案
如果您通过第二个数组作为参数传递给 array_map
这应该工作将包含尽可能多的 ENT_QUOTES
元素的元素数 $阵列
:
This should work if you pass a second array as parameter to array_map
that will contain as many ENT_QUOTES
elements as your number of elements in $array
:
$quote_style = ENT_QUOTES;
$array = array('"',"'","''''''''''''\"");
$ent_quotes_array = array($quote_style, $quote_style, $quote_style);
$array = array_map('htmlspecialchars', $array, $ent_quotes_array);
print_r($array);
或者,更优雅一点:
Or, a little bit more elegant:
$array = array('"',"'","''''''''''''\"");
$ent_quotes_array = array_fill(0, sizeof($array), ENT_QUOTES);
$array = array_map('htmlspecialchars', $array, $ent_quotes_array);
这篇关于有没有一种方法来发送参数到一个回调函数不首先创造我自己的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!