PHP htmlspecialchars 错误

发布于 2024-08-02 04:35:05 字数 316 浏览 6 评论 0原文

为什么会

$trader_details = array_walk($trader_details, 'htmlspecialchars');

出现这个错误?

Severity: Warning
Message: htmlspecialchars() expects parameter 2 to be long, string given

afaik htmlspecialchars 除了输入字符串之外只有可选参数?这在 codeigniter 中运行,

谢谢

why would this

$trader_details = array_walk($trader_details, 'htmlspecialchars');

give this error?

Severity: Warning
Message: htmlspecialchars() expects parameter 2 to be long, string given

afaik htmlspecialchars only has optional parameters apart from the input string? this running in codeigniter

thx

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

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

发布评论

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

评论(7

剑心龙吟 2024-08-09 04:35:05

传递给 array_walk 的回调函数期望第二个参数是数组元素的键:

通常,funcname 采用两个参数。 数组参数的值是第一个,键/索引是第二个。

但是 htmlspecialchars 期望第二个参数是引用样式(通常由整数类型的 ENT_* 常量 之一)。

请尝试 array_map 。它只使用数组的值。

The callback function passed to array_walk expects the second parameter to be the key of the array element:

Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second.

But htmlspecialchars expects the second parameter to be the quoting style (typically specified by one of the ENT_* constants of the type integer).

Try array_map instead. It just uses the array’s values.

紅太極 2024-08-09 04:35:05

array_walk 默认传递 2 个参数。第一个是数组项值,第二个是数组项键。它尝试将数组键作为第二个参数传递给 htmlspecialchars,它期望第二个参数是定义要使用的引用样式的整数。

array_walk passes 2 arguments by default. The first is the array item value, the second is the array item key. It's trying to pass the array key as the second argument to htmlspecialchars which expects the second argument to be an integer defining the quoting style to use.

躲猫猫 2024-08-09 04:35:05

http://uk.php.net/array_walk 说:

funcname

通常,funcname 采用两个参数。数组参数的值是第一个,键/索引是第二个

您可能正在寻找 aray_map
另请注意,htmlspecialchars() 默认使用 iso-8859-1 作为编码。如果您的输出是 utf-8 编码的,您必须将该信息作为第三个参数传递给 htmlspecialchars。否则结果可能是错误的。
PHP 5.3:

$foo = array_map(
  function($x) { return htmlspecialchars($x, ENT_QUOTES, 'utf-8'); },
  $trader_details
);

http://uk.php.net/array_walk says:

funcname

Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second.

You're probably looking for aray_map.
Also note that htmlspecialchars() uses iso-8859-1 as encoding by default. If your output is e.g. utf-8 encoded you have to pass that information as third parameter to htmlspecialchars. Otherwise the result may be wrong.
php 5.3:

$foo = array_map(
  function($x) { return htmlspecialchars($x, ENT_QUOTES, 'utf-8'); },
  $trader_details
);
北凤男飞 2024-08-09 04:35:05

我假设 $trader_details 是一个字符串数组? htmlspecialchars() 的第二个参数是一个整数类型,用于要使用的特定引用样式。

您可能想使用 array_map。如果 $trader_details 是一个二维数组,请发布它,以便我们可以看到您想要做什么。

I assume $trader_details is an array of strings? htmlspecialchars()'s second parameter is an integer type, for the specific quotestyle to be used.

You probably want to use array_map. If $trader_details is a two-dimensional array, please post it so we can see what you're trying to do.

南街九尾狐 2024-08-09 04:35:05

array_walk 将 2 个参数传递给您的方法 (htmlspecialchars),第一个是当前数组元素的值,第二个是当前元素的 key

所以,如果

$trader_details = array('key' => 'value');

then

$trader_details = array_walk($trader_details, 'htmlspecialchars');

调用

htmlspecialchars('value', 'key')

这是不正确的,htmlspecialchars 要求第二个参数是整数 - int $quote_style

array_walk passes 2 arguments to your method (htmlspecialchars), first is value of the current array element, second is the key of the current element.

so, if

$trader_details = array('key' => 'value');

then

$trader_details = array_walk($trader_details, 'htmlspecialchars');

calls

htmlspecialchars('value', 'key')

And that is incorrect, htmlspecialchars requires the second parameter to be an integer - int $quote_style

枕头说它不想醒 2024-08-09 04:35:05

我不认为它会做你想要的,即使它有效。

htmlspecialchars() 函数不会修改字符串,它只是返回经过修改的新字符串。数组遍历不会有任何影响。

I don't think it would do what you want, even if it worked.

The htmlspecialchars() function does not modify the string, it just returns a new string with the modifications. The array walk would not have any affect.

混吃等死 2024-08-09 04:35:05

错误很明显... array_walk 的第二个参数是关于函数回调的,并且函数需要有 2 个参数。第一个用于值,第二个用于键..

The error is obvious... array_walk's second argument is about function call back, and function needs to have 2 parameters. first one for value and second for key..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文