Home >Backend Development >PHP Tutorial >PHP returns the function addcslashes() which refers to characters preceded by a backslash
Example
Add a backslash before the character "W":
<?php $str = addcslashes("Hello World!","W"); echo($str); ?>
Definition and usage
addcslashes() function returns the specified string with a backslash added before the character.
Note: The addcslashes() function is case-sensitive.
Note: Apply addcslashes() to 0 (NULL), r (carriage return), n (line feed), t (form feed), f (tab) and v (vertical tab) Be careful when doing so. In PHP, \0, \r, \n, \t, \f and \v are predefined escape sequences.
Syntax
addcslashes(string,characters)
Parameters | Description |
string | Required. Specifies the string to be escaped |
characters | Required. Specifies the characters or character range to be escaped. |
Technical details
Return value: | Returns the escaped string. |
PHP version: | 4+ |
<?php $str = "Welcome to my humble Homepage!"; echo $str."<br>"; echo addcslashes($str,'m')."<br>"; echo addcslashes($str,'H')."<br>"; ?>Add a backslash to a range of characters in the string:
<?php $str = "Welcome to my humble Homepage!"; echo $str."<br>"; echo addcslashes($str,'A..Z')."<br>"; echo addcslashes($str,'a..z')."<br>"; echo addcslashes($str,'a..g'); ?>The following is a brief introduction to the usage of these two functions: string addcslashes(string str,string charlist)The first parameter str is the original string of the lost object The second parameter charlist indicates which characters of the original string need to be preceded Add the character "\". string
stripcslashes(string str)
Remove "\" in the string. In addition, using theaddslashes function can also directly escape "'".
The example is as follows:<?php $sql = "update book set bookname='let's go' where bookid=1"; echo $sql."<br/>"; $new_sql = addcslashes($sql,"'"); echo $new_sql."<br/>"; $new_sql_01 = stripcslashes($new_sql); echo $new_sql_01."<br/>"; echo addslashes($sql); ?>The running result is as follows:
update book set bookname='let's go' where bookid=1 update book set bookname=\'let\'s go\' where bookid=1 update book set bookname='let's go' where bookid=1 update book set bookname=\'let\'s go\' where bookid=1
The above is the detailed content of PHP returns the function addcslashes() which refers to characters preceded by a backslash. For more information, please follow other related articles on the PHP Chinese website!