PHP htmlspecialchars 错误
为什么会
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
传递给 array_walk 的回调函数期望第二个参数是数组元素的键:
但是
htmlspecialchars
期望第二个参数是引用样式(通常由整数类型的ENT_*
常量 之一)。请尝试
array_map
。它只使用数组的值。The callback function passed to
array_walk
expects the second parameter to be the key of the array element:But
htmlspecialchars
expects the second parameter to be the quoting style (typically specified by one of theENT_*
constants of the type integer).Try
array_map
instead. It just uses the array’s values.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.
http://uk.php.net/array_walk 说:
您可能正在寻找 aray_map。
另请注意,htmlspecialchars() 默认使用 iso-8859-1 作为编码。如果您的输出是 utf-8 编码的,您必须将该信息作为第三个参数传递给 htmlspecialchars。否则结果可能是错误的。
PHP 5.3:
http://uk.php.net/array_walk says:
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:
我假设 $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.
array_walk 将 2 个参数传递给您的方法 (htmlspecialchars),第一个是当前数组元素的值,第二个是当前元素的 key。
所以,如果
then
调用
这是不正确的,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
then
calls
And that is incorrect, htmlspecialchars requires the second parameter to be an integer - int $quote_style
我不认为它会做你想要的,即使它有效。
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.
错误很明显... 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..