note 77032 deleted from function.addslashes by felipe
| From: | felipe@php.net | Date: | Fri, 18 Apr 2008 22:35:23 +0000 |
| Subject: | note 77032 deleted from function.addslashes by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-138521@lists.php.net to get a copy of this message | ||
Note Submitter: Lancelight
----
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;
}
?>