PHP addslashes() function
Example
Add backslashes before each double quote ("}
<?php $str = addslashes('Shanghai is the "biggest" city in China.'); echo($str); ?>
Definition and Usage
The addslashes() function returns a string with backslashes added before the predefined characters.
The predefined characters are:
- Single quotes ('}
- Double quotes ("}
- Backslash (\)
- NULL
Tip:This function can be used to prepare strings for storage in a database and for database query statements.
Note:By default, PHP automatically runs addslashes() on all GET, POST, and COOKIE data. Therefore, you should not use addslashes() on already escaped strings, as this would result in double escaping. In such cases, you can use the function get_magic_quotes_gpc() to check.
Syntax
addslashes(string)
Parameter | Description |
---|---|
string | Required. Specifies the string to be escaped. |
Technical Details
Return Value: | Returns the escaped string. |
PHP Version: | 4+ |
More Examples
Example 1
Add backslashes to predefined characters in a string:
<?php $str = "Who's Bill Gates?"; echo $str . " This is not safe in a database query.<br>"; echo addslashes($str) . " This is safe in a database query."; ?>