1. addslashes() function
1. The addslashes() function adds a backslash before the specified predetermined character. Syntax: addslashes(str);
2. The parameter is a string
3. There are four kinds of predefined characters: single quotation mark ('), double quotation mark ("), Backslash (\) and NULL
4. For example:
<?php $str="Who's John Adams?"; echo $str."This is not safe in a database query.<br/>";//输出:Who's John Adams?This is not safe in a database query. echo addslashes($str)."This is safe in a database query.";//输出:Who\'s John Adams?This is sage in a database query. ?>
12345678910
2. addcslashes() function
1.addcslashes () function adds a backslash before the specified character.
Syntax: addcslashes(str,chararcters);
2. The parameter str is required and specifies the character to be checked. String, and character is optional, specifying the characters or character range affected by addcslashes()
3. Example 1:
<?php $str="Hello,my name is John Adams."; echo $str; //输出:Hello,my name is John Adams.echo addcslashes($str,'m'); //输出: Hello,\my na\me is John Ada\ms.echo addcslashes($str,'J'); //输出:Hello,my name is \John Adams ?>
123456789101112
Example. 2.
<?php$str="Hello,my name is John Adams."; echo $str; //输出:Hello,my name is John Adams. echo addcslashes($str,'A..Z'); //输出:\Hello,my name is \John \Adams. echo addcslashes($str,'a..z'); //输出:H\e\l\l\o,\m\y \n\a\m\e \i\s J\o\h\n A\d\a\m\s. echo addcslashes($str,'a..h'); //输出:H\ello,my n\am\e is Jo\hn A\d\ams. ?>
1234567891011121314
Note: The addcslashes() function is case-sensitive for the specified character or character range
in the character ". Add a backslash before W":
<?php $str = addcslashes("Hello World!","W"); echo($str); ?>
Definition and usage
addcslashes() function returns a string with a backslash before the specified character.
Comment: The addcslashes() function is case-sensitive.
Comments: 0 (NULL), r (carriage return), n (line feed), t (form feed), f (tab character). ) and v (vertical tab) be careful when applying addcslashes(). In PHP, \0, \r, \n, \t, \f and \v are predefined escape sequences
#. ##Syntaxaddcslashes(string,characters)
Description
string Required. characters Required. Or a range of characters. Technical DetailsReturn value:
Returns the escaped string Adds inversion to specific characters in the string. Slash:<?php $str = "Welcome to my humble Homepage!"; echo $str."<br>"; echo addcslashes($str,'m')."<br>"; echo addcslashes($str,'H')."<br>"; ?>
<?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 above is the detailed content of PHP returns the string function addslashes() that adds a backslash before the specified character.. For more information, please follow other related articles on the PHP Chinese website!