The difference and comparison between php addslashes of and addclashes of are analyzed

  • 2020-06-19 09:55:02
  • OfStack

PHP addcslashes () function
Definition and usage
The addcslashes() function adds a backslash before the specified character.
grammar
addcslashes(string,characters) parameter description
string required. Specifies the string to check.
characters optional. Specifies the characters or range of characters affected by addcslashes().
Hints and comments
Note: Be careful when applying addcslashes() to 0, r, n, and t. In PHP, \0, \r, \n and \t are predefined escape sequences.
The instance
Example 1
In this case, we want to add a backslash to a specific character in the string:
< ?php
$str = "Hello, my name is John Adams.";
echo $str;
echo addcslashes($str,'m');
echo addcslashes($str,'J');
? >
Output:
Hello, my name is John Adams.
Hello, \my na\me is John Ada\ms.
Hello, my name is \John Adams.

The use of the function addslashes () :
PHP addslashes () function
Definition and usage
The addslashes() function adds a backslash before the specified predefined character.
These predefined characters are:
The & # 8226; Single quotes (')
The & # 8226; Double quotation marks (")
The & # 8226; The backslash (\)
•NULL
grammar
addslashes(string) parameter description
string required. Specifies the string to check.
Hints and comments
Tip: This function can be used to prepare the appropriate string for the string stored in the database as well as the database query statement.
Note: By default, PHP instruction magic_quotes_gpc is on and runs addslashes() automatically for all GET, POST and COOKIE data. Do not use addslashes() on strings that have been escaped by magic_quotes_gpc, as this will result in double escape. This can be detected using the function get_magic_quotes_gpc().
example
In this case, we want to add backslashes to the predefined characters in the string:
< ?php
$str = "Who's John Adams?";
echo $str . " This is not safe in a database query. < br / > ";
echo addslashes($str) . " This is safe in a database query.";
? >
Output:
Who's John Adams? This is not safe in a database query.
Who \ 's John Adams? This is safe in a database query. Each of them has a corresponding method to remove the backslash they added: stripcslashes() and stripslashes().


Related articles: