PHP htmlspecialchars error
Asked Answered
B

7

3

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

Bevy answered 22/8, 2009 at 21:15 Comment(1)
Removed the codeigniter tag since it does not appear to be contributing to the error.Motor
F
2

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.

Fianna answered 22/8, 2009 at 21:17 Comment(2)
Severity: Warning Message: array_map() [function.array-map]: The first argument, 'Array', should be either NULL or a valid callbackBevy
@Bevy : take a look at the manual : array_map wants parameters in another order than array_walk (yeah... that's one not so nice thing with PHP ^^ ) ; see php.net/array_walk and php.net/array_map : array_map expects callback first, and array secondRaymer
L
2

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.

Lysine answered 22/8, 2009 at 21:19 Comment(0)
C
1

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
);
Cowans answered 22/8, 2009 at 21:20 Comment(0)
U
0

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.

Unsling answered 22/8, 2009 at 21:20 Comment(0)
O
0

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

Olmsted answered 22/8, 2009 at 21:21 Comment(0)
O
0

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.

Oedipus answered 22/8, 2009 at 21:24 Comment(0)
F
-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..

Fatling answered 22/8, 2009 at 21:20 Comment(1)
It's not obvious how to send multiple parameters to a function using array_walk. The OP is not the first to run into this problem.Vaccination

© 2022 - 2024 — McMap. All rights reserved.