PHP additives() وظيفة
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 predefined characters.
The predefined characters are:
- Single Quotes ('}
- Double Quotes ("}
- Backslash (\)
- NULL
Tip:This function can be used to prepare strings for storage in databases 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 will cause 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: | Return 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."; ?>