We know that in php, The addslashes function is a secure filter function, which can be used to escape the variables to securely store them into the database. However, if we pass parameters in the form of arrays, you cannot directly use this function. Let's take a look at the implementation method. We know that in php, The addslashes function is a secure filter function, which can be used to escape the variables to securely store them into the database. However, if we pass parameters in the form of arrays, you cannot directly use this function. Let's take a look at the implementation method.
Script ec (2); script
I. functions mainly involve the addslashes method.
The addslashes () function adds a backslash before the specified predefined character.
The predefined characters are:
• Single quotes (')
• Double quotation marks (")
• Backslash (\)
• NULL
Ii. array_map description
The array_map () function returns the array after the User-Defined Function is applied. The number of parameters accepted by the callback function should be the same as the number of arrays passed to the array_map () function.
If the passed parameters are in the array format, use the array_map method to escape the parameters.
We are developing variables such as $ _ GET and $ _ POST.
| The Code is as follows: |
|
|
/**
* Recursively escape special characters in a variable
*
* @ Access public
* @ Param mix $ value
*
* @ Return mix
*/
Function addslashes_deep ($ value)
{
If (empty ($ value ))
{
Return $ value;
}
Else
{
Return is_array ($ value )? Array_map ('addslashes _ deep ', $ value): addslashes ($ value );
}
} |
If it is an array, the security escape cannot be completed. The following is an example. Let's take a look.
Method 3,You can refer to this recursive method to implement other functions. The Code is as follows:
| The Code is as follows: |
|
|
$ Arr = array ('A "AA', array (" c 'd ", array ('E" F ')));
Function changes ($ arr ){
Foreach ($ arr as $ k =>$ v ){
If (is_string ($ v )){
$ Arr [$ k] = addslashes ($ v );
} Else if (is_array ($ v) {// escape if it is an array.
$ Arr [$ k] = changes ($ v );
}
}
Return $ arr;
}
Print_r (changes ($ arr ));
?>
The input result is as follows:
Array
(
[0] => a \ "aa
[1] => Array
(
[0] => c \ 'd
[1] => Array
(
[0] => e \ "f
)
)
) |
The principle is very simple, that is, to use foreach to traverse the array values.