views:

384

answers:

1

I'm trying to use the stripslashes_deep function in the PHP documentation as a method, but when I pass it an array, it doesn't work. It does, however, work with a string. Can anyone point out why it might not be working? Magic quotes are on because I'm testing for that possibility, but like I said, it works for a string and I'm also not testing $_POST, or $_GET data. Here's the code:

protected function stripslashes_deep($input){
 return is_array($input) ? array_map($this->stripslashes_deep, $input) : stripslashes($input);
}

The string 'O\'Reilly' is converted to 'O'Reilly', but the array below remains unchanged:

Array ( [0] => Array ( [userId] => 23 [0] => 23 [username] => O\'Reilly [1] => O\'Reilly [userFirstName] => Bill [2] => Bill [userLastName] => O\'Reilly [3] => O\'Reilly ) )

Any help would be greatly appreciated.

Edit:

The array is coming from the database and has slashes because it was inserted with bound parameters by the PDO object. I extended PDO and I'm trying to strip the slashes automatically when I fetch the array so I don't have to call stripslashes every single time I output data. If anyone knows of a better way to do that, I'm definitely open to suggestions. I didn't see any sort of unquote method for PDO.

+6  A: 
array_map($this->stripslashes_deep, $input) : stripslashes($input);

Should be:

array_map(array($this, 'stripslashes_deep'), $input) : stripslashes($input);

If your intend is to undo magic-quotes, you shouldn't implement it recursively btw., since it has some performance and security issues. See: http://talks.php.net/show/php-best-practices/26

troelskn
I'll be checking into that, thanks for the link.
VirtuosiMedia