有没有一种方法可以将参数发送到回调函数中,而无需先创建自己的函数?

发布于 2024-12-20 15:39:46 字数 561 浏览 4 评论 0原文

我有一个值数组,我想通过 htmlspecialchars 运行,但带有这样的参数:

$param = htmlspecialchars($param, ENT_QUOTES);

问题是,我有一个值数组,我想在其上运行 htmlspecialchars:

$array = array_map('htmlspecialchars', $array);

我想知道是否有将 ENT_QUOTES 传递到 array_map 回调的方法?

我总是可以使用我自己的使用 htmlspecialchars 的函数,但如果已经有一种方法可以做到这一点,那就太好了。


在下面的答案之后,这是我的最终结果:

$array = array_map('htmlspecialchars', $array, array_fill(0, count($array), ENT_QUOTES));

它只是用与 $array 具有的值一样多的值填充数组,并且用 ENT_QUOTE 填充。

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);

The problem is, I have an array of values that I want to run htmlspecialchars on:

$array = array_map('htmlspecialchars', $array);

and I would like to know if there is a way to pass ENT_QUOTES into the array_map callback?

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));

Which simply fills an array with as many values as $array has and it's filled with ENT_QUOTE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

美人骨 2024-12-27 15:39:46

如果您将第二个数组作为参数传递给 array_map,该数组将包含与 $array 中的元素数量一样多的 ENT_QUOTES 元素,那么这应该有效:

$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);

或者,更优雅一点:

$array = array('"',"'","''''''''''''\"");
$ent_quotes_array = array_fill(0, sizeof($array), ENT_QUOTES);
$array = array_map('htmlspecialchars', $array, $ent_quotes_array);

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);
靑春怀旧 2024-12-27 15:39:46

这是我的输出辅助函数......

function change_values_for_encode_output(&$item, $key) {
    $item = htmlentities($item, ENT_QUOTES);
}

function encode_output_vars($vars) {
    if(is_array($vars)) {
        array_walk_recursive($vars, 'change_values_for_encode_output');
        return $vars;
    }
    else {
        $vars = htmlentities($vars, ENT_QUOTES);
                    return $vars;
    }
}

Here is my output helper function...

function change_values_for_encode_output(&$item, $key) {
    $item = htmlentities($item, ENT_QUOTES);
}

function encode_output_vars($vars) {
    if(is_array($vars)) {
        array_walk_recursive($vars, 'change_values_for_encode_output');
        return $vars;
    }
    else {
        $vars = htmlentities($vars, ENT_QUOTES);
                    return $vars;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文