PHP:如何在数组数组上执行htmlspecialchar()?

Ted*_*dyR 14 html php arrays web-applications

如何htmlspecialchars()在数组对象数组上运行PHP函数?

我有以下代码:

$result_set = Array
(
    [0] => Array
        (
            [home_id] => 1
            [address] => 4225 Nasmyth Dr
            [city] => Plano
            [state] => TX
            [zip] => 76798
        )

    [1] => Array
        (
            [home_id] => 8
            [address] => 4229 Nasmyth Dr
            [city] => Plano
            [state] => TX
            [zip] => 75093
        )
);

// this doesn't work since $result_set is an array of arrays and htmlspecialchars is expecting a string
htmlspecialchars($result_set, ENT_QUOTES, 'UTF-8')); 
Run Code Online (Sandbox Code Playgroud)

更新:

请注意,尽管下面有很多答案,但它们都不适用于阵列数组.以下答案仅适用于简单数组.

我尝试了以下,但它不起作用:

array_walk_recursive($result_set, "htmlspecialchars", array(ENT_QUOTES,'UTF-8'))
Run Code Online (Sandbox Code Playgroud)

我收到以下错误: htmlspecialchars() expects parameter 2 to be long, string given


更新2

当我尝试:

function cleanOutput(&$value) {
    return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
print_r($result_set);
print('-------');
print_r(array_walk_recursive($result_set, "cleanOutput"));
Run Code Online (Sandbox Code Playgroud)

我得到以下,不受欢迎的输出:

Array
(
    [0] => Array
        (
            [home_id] => 1
            [address] => 4225 Nasmyth Dr
            [city] => Plano
            [state] => TX
            [zip] => 76798
        )
    [1] => Array
        (
            [home_id] => 8
            [address] => 4229 Nasmyth Dr
            [city] => Plano
            [state] => TX
            [zip] => 75093
        )
)
-------1
Run Code Online (Sandbox Code Playgroud)

更新3

当我尝试:

function cleanOutput(&$value) {
    return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
$result_set = Array
    (
        [0] => Array
            (
                [home_id] => 1
                [address] => 4225 Nasmyth Dr
                [city] => Plano
                [state] => TX
                [zip] => 76798
            )

        [1] => Array
            (
                [home_id] => 8
                [address] => 4229 Nasmyth Dr
                [city] => Plano
                [state] => TX
                [zip] => 75093
            )
    );

$cleanedOutput = array();
foreach ($result_set as $rs) {
    $cleaned[] = array_map("cleanOutput", $rs);
}
print_r($cleanedOutput);
Run Code Online (Sandbox Code Playgroud)

我得到以下,不受欢迎的结果:

{'homes' : []}
Run Code Online (Sandbox Code Playgroud)

Sam*_*son 23

您可以使用array_map()在每个条目上运行该方法.

$cleaned = array_map("htmlspecialchars", $myArray);
Run Code Online (Sandbox Code Playgroud)

如果需要传递参数htmlspecialchars(),可以将其替换为自己的自定义函数:

function myFunc($a) {
  return htmlspecialchars($a, ENT_QUOES);
}

$cleaned = array_map("myFunc", $myArray);
Run Code Online (Sandbox Code Playgroud)

考虑到你正在处理一个数组数组,而不是一个字符串数组,你需要遍历外部数组来获取你的字符串:

$cleaned = array();
foreach ($result_set as $rs) {
  foreach ($rs as $r) {
    $cleaned[] = array_map("htmlspecialchars", $r);
  }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用array_walk_recursive():

array_walk_recursive($myArray, "htmlspecialchars");
Run Code Online (Sandbox Code Playgroud)

请注意,此方法通过引用更改$ myArray对象,因此无需将输出分配给新变量.


JW.*_*JW. 16

function filter(&$value) {
  $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
array_walk_recursive($result_set, "filter");
print_r($result_set);
Run Code Online (Sandbox Code Playgroud)

  • 在UPDATE 2中,您仍然打印array_walk_recursive的返回值,这不是您想要的.只需打印$ result_set.慢下来,阅读array_walk_recursive的手册页...你会看到它是如何工作的.我修改了上面的例子来打印数组. (3认同)

Ant*_*los 6

本页上的许多答案要么不充分、过时,要么对array_maparray_walk_recursive使用错误的参数。这是一个函数,它将递归地修复数组中的所有标量值。

\n\n

htmlspecialchars_recursive()

\n\n
<?php\n\nfunction htmlspecialchars_recursive ($input, $flags = ENT_COMPAT | ENT_HTML401, $encoding = \'UTF-8\', $double_encode = false) {\n    static $flags, $encoding, $double_encode;\n    if (is_array($input)) {\n        return array_map(\'htmlspecialchars_recursive\', $input);\n    }\n    else if (is_scalar($input)) {\n        return htmlspecialchars($input, $flags, $encoding, $double_encode);\n    }\n    else {\n        return $input;\n    }\n}\n\n$test = array(\n    0 => array(\n        \'test-1\' => \'testing <p>html tag</p> will be fixed\',\n        \'test-2\' => \'&reg; valid and will be left intact\',\n        \'test-3\' =>  \'\xc2\xa9 2080 kept intact\'\n    ),\n    1 => array(\n        \'test-4\' => array(\n            \'test-5\' => \'deeper fix on <p>html tag</p> test\',\n            \'test-6\' => \'&reg; will be left intact\',\n            \'test-7\' =>  \'\xc2\xa9 2080 kept intact\'\n        )\n    )\n);\n\nprint_r(htmlspecialchars_recursive($test));\n\n?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出

\n\n
Array\n(\n    [0] => Array\n        (\n            [test-1] => testing &lt;p&gt;html tag&lt;/p&gt; will be fixed\n            [test-2] => &reg; valid and will be left intact\n            [test-3] => \xc2\xa9 2080 kept intact\n        )\n\n    [1] => Array\n        (\n            [test-4] => Array\n                (\n                    [test-5] => deeper fix on &lt;p&gt;html tag&lt;/p&gt; test\n                    [test-6] => &reg; will be left intact\n                    [test-7] => \xc2\xa9 2080 kept intact\n                )\n\n        )\n\n)\n
Run Code Online (Sandbox Code Playgroud)\n