简体   繁体   English

PHP htmlspecialchars错误

[英]PHP htmlspecialchars error

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? 除了输入字符串之外,afaik htmlspecialchars只有可选参数吗? this running in codeigniter 这在codeigniter中运行

thx 谢谢

The callback function passed to array_walk expects the second parameter to be the key of the array element: 传递给array_walk的回调函数需要第二个参数作为数组元素的键:

Typically, funcname takes on two parameters. 通常, funcname采用两个参数。 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). 但是htmlspecialchars期望第二个参数是引用样式(通常由整数类型的ENT_*常量之一指定)。

Try array_map instead. 请尝试使用array_map It just uses the array's values. 它只使用数组的值。

array_walk passes 2 arguments by default. array_walk默认传递2个参数。 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. 它试图将数组键作为第二个参数传递给htmlspecialchars,它希望第二个参数是一个定义要使用的引用样式的整数。

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

funcname funcname的
Typically, funcname takes on two parameters. 通常,funcname采用两个参数。 The array parameter's value being the first, and the key/index second . 数组参数的值是第一个, 键/索引的第二个

You're probably looking for aray_map . 你可能正在寻找aray_map Also note that htmlspecialchars() uses iso-8859-1 as encoding by default. 另请注意,htmlspecialchars()默认使用iso-8859-1作为编码。 If your output is eg utf-8 encoded you have to pass that information as third parameter to htmlspecialchars. 如果您的输出是例如utf-8编码,则必须将该信息作为第三个参数传递给htmlspecialchars。 Otherwise the result may be wrong. 否则结果可能是错误的。
php 5.3: php 5.3:

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

I assume $trader_details is an array of strings? 我假设$ trader_details是一个字符串数组? htmlspecialchars ()'s second parameter is an integer type, for the specific quotestyle to be used. htmlspecialchars ()的第二个参数是整数类型,用于要使用的特定quotestyle。

You probably want to use array_map . 您可能想要使用array_map If $trader_details is a two-dimensional array, please post it so we can see what you're trying to do. 如果$ trader_details是一个二维数组,请发布它,以便我们可以看到你想要做的事情。

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. array_walk将2个参数传递给您的方法(htmlspecialchars),第一个是当前数组元素的值,第二个是当前元素的

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 这是不正确的,htmlspecialchars要求第二个参数是一个整数 - int $ quote_style

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. htmlspecialchars()函数不会修改字符串,只返回带有修改的新字符串。 The array walk would not have any affect. 数组遍历不会有任何影响。

The error is obvious... array_walk's second argument is about function call back, and function needs to have 2 parameters. 错误很明显... array_walk的第二个参数是关于函数回调,而函数需要有2个参数。 first one for value and second for key.. 第一个用于值,第二个用于键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM