PHP addslashes () function
Examples
In each double quotation mark ( ") before adding a backslash:
<?php
$str = addslashes('What does "yolo" mean?');
echo($str);
?>
$str = addslashes('What does "yolo" mean?');
echo($str);
?>
Running instance »
Definition and Usage
addslashes () function returns before the predefined character string to add the backslash.
Predefined characters are:
- apostrophe(')
- Double quotes(")
- Backslash (\)
- NULL
Tip: This function can be used to store the string in the database and database query prepare appropriate string.
NOTE: By default, PHP directive magic_quotes_gpc is on, all GET, POST and COOKIE data automatically run addslashes ().Do not have been escaped with magic_quotes_gpc string using addslashes (), because this would lead to double-escape. You can use the function get_magic_quotes_gpc When this happens () were detected.
grammar
addslashes( string )
参数 | 描述 |
---|---|
string | 必需。规定要转义的字符串。 |
technical details
return value: | Returns the escaped string. |
---|---|
PHP version: | 4+ |
More examples
Example 1
Adding to a string of predefined characters backslash:
<?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.";
?>
$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.";
?>
Running instance »
PHP String Reference