note 77032 added to function.addslashes
| From: | Lancelight at osu1 dot php dot net | Date: | Sat, 11 Aug 2007 21:14:49 +0000 |
| Subject: | note 77032 added to function.addslashes | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129414@lists.php.net to get a copy of this message | ||
I found a very odd behavior when you combine addslashes and array_map in combination with an html
form that has arrays in it. I dont know how to explain it other than showing the code for it. I
believe this is probably a bug with array_map not picking up the array in the array that it
received? Or maybe we just need a new PHP function for add/stripslashed that is array capable.
<?
$post_new = array_strip_slashes($_POST);
$_POST = array_map('stripslashes', $_POST);
//This is what the PHP array_map/addslashes does
echo "\$_POST: ";
print_r($_POST);
echo "\n<br>";
//This is what my work-around does
echo "\$post_new: ";
print_r($post_new);
?>
<form action=<? echo $_SERVER['PHP_SELF']; ?> method=POST>
<select name="categories[]" multiple>
<option value="number '1'"> number '1'</option>
<option value="number '2'"> number '2'</option>
<option value="number '3'"> number '3'</option>
<option value="number '4'"> number '4'</option>
<input type=submit name=submit value=submit>
</form>
<?
function array_add_slashes($array)
{
if (is_array($array))
{
foreach ($array AS $key => $value)
{
if (!is_array($value))
{
//echo "$key -> $value <br>";
$value = addslashes($value);
$key = addslashes($key);
$new_arr[$key] = $value;
}
if (is_array($value))
{
$new_arr[$key] = array_add_slashes($value);
}
}
}
return $new_arr;
}
function array_strip_slashes($array)
{
if (is_array($array))
{
foreach ($array AS $key => $value)
{
if (!is_array($value))
{
//echo "$key -> $value <br>";
$value = stripslashes($value);
$key = stripslashes($key);
$new_arr[$key] = $value;
}
if (is_array($value))
{
$new_arr[$key] = array_add_slashes($value);
}
}
}
return $new_arr;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.218.145.239
----
Manual Page -- http://www.php.net/manual/en/function.addslashes.php
Edit -- https://master.php.net/note/edit/77032
Del: integrated -- https://master.php.net/note/delete/77032/integrated
Del: useless -- https://master.php.net/note/delete/77032/useless
Del: bad code -- https://master.php.net/note/delete/77032/bad+code
Del: spam -- https://master.php.net/note/delete/77032/spam
Del: non-english -- https://master.php.net/note/delete/77032/non-english
Del: in docs -- https://master.php.net/note/delete/77032/in+docs
Del: other reasons-- https://master.php.net/note/delete/77032
Reject -- https://master.php.net/note/reject/77032
Search -- https://master.php.net/manage/user-notes.php