What are the functions of the addslashes() function and stripslashes() function?
addslashes(): Add backslashes before certain predefined characters in the input string. This processing is for the needs of database query statements, etc. These predefined characters are: single quote ('), double quote ("), backslash (\), NULL.
stripslashes(): Remove the additions added by the addslashes() function backslash. This function is used to clean the data retrieved from the database or HTML form (if there are two consecutive backslashes, remove one and keep one; if there is only one backslash, remove it directly.)
ps: By default, the PHP command magic_quotes_gpc is on and addslashes() is automatically run on all GET, POST and COOKIE data. Do not use addslashes() on strings that have been escaped by magic_quotes_gpc. , because this will cause double-level escaping. When encountering this situation, you can use the function get_magic_quotes_gpc() for detection. Example:
if (get_magic_quotes_gpc()){
code....
}
Understanding their functions, let’s take a look at their syntax:
addslashes() function: before certain predefined characters in the input string Add backslash
Syntax:
addslashes(string)
Parameter details:
string Specifies the characters to be escaped String.
Example
The following example adds a backslash to the predefined characters in the string. The code is as follows:
<?php $str = "Who's Peter Griffin?"; echo $str . " This is not safe in a database query.<br>"; echo addslashes($str) . " This is safe in a database query."; ?>
Code operation Result:
##stripslashes(): Removes backslashes added by addslashes() function .
##Syntaxstripslashes(string)
string Specifies the string to be checked. Example
Remove the backslash, the code is as follows<?php echo stripslashes("Who\'s Bill Gates?"); ?>
[PHP video tutorial recommendation]:
《php.cn Dugu Jiujian (4)-php video tutorial
》The above is the detailed content of Detailed explanation of php addslashes() function and stripslashes() function examples. For more information, please follow other related articles on the PHP Chinese website!