The
PHP addcslashes () function
definition and usage
addcslashes () function adds a backslash before the specified character. The
syntax
addcslashes (string,characters) parameter description
string is required. Specify the string to check.
Characters optional. Specify the character or range of characters affected by addcslashes ().
Hints and comments
notes: Be careful when applying addcslashes () to 0,r,n and T. In PHP, \0,\r,\n and \ t are predefined escape sequences.
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, the My name is \john Adams.
and the use of function addslashes ():
PHP addslashes () function
Definition and usage
addslashes () function to add a backslash before the specified predefined character.
These predefined characters are:
• Single quotes (')
• double quotes (")
• backslash (\)
null
syntax
addslashes ( String) parameter to describe the
string required. Specify the string to check.
hint and comment
hint: This function can be used to prepare the appropriate string for the string stored in the database and for the database query statement.
Note: By default, the PHP directive MAGIC_QUOTES_GPC to on and automatically runs Addslashes () for all get, POST, and COOKIE data. Do not use Addslashes () on strings that have been escaped by MAGIC_QUOTES_GPC, because this can result in a double escape. You can use the function GET_MAGIC_QUOTES_GPC () for instrumentation when this situation is encountered.
Example
In this case, we want to add a backslash to the predefined characters in the string:
<?php
$str = "Who's John Adams?";
Echo $str. "This isn't safe in a database query.<br/>";
Echo addslashes ($STR). "This is safe in a database query.";
output:
who John Adams? This isn't safe in a database query.
Who\ ' s John Adams? This is safe in a database query. They all have a corresponding way to remove the backslash they add, respectively: Stripcslashes () and Stripslashes ().