<metahttp-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> automatically escapes and restores strings </title>
<styletype= "Text/css" >
<!--
Body {
Background-color: #FFCCFF;
}
-->
</style>
<body>
<?php
$STR = "SELECT * from Tb_book where bookname = ' PHP project development full record '";
echo $str. " <br> ";
$a = addslashes ($STR);//to escape special characters in a string
echo $a. " <br>/Output escaped characters
$b = stripslashes ($a);//Restore the escaped character
echo $b. " <br> "//Fu Yuanyi Word output
?>
</body>
Run Result:
SELECT * from tb_book where bookname = ' PHP project development full Record '
SELECT * from tb_book where bookname = \ ' PHP project development full record \ '
SELECT * from tb_book where bookname = ' PHP project development full Record '
Instance 2
"Http://www.w3.org/TR/html4/loose.dtd" >
<metahttp-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> escape, restore </title> for a specified range of strings
<styletype= "Text/css" >
<!--
Body {
Background-color: #FFCCCC;
}
-->
</style><body>
<?php
$a = "Programming body ABC ' Network";//to escape the characters in the specified range
$b =addcslashes ($a, "programming experience Network");//Escape the specified string
echo "Escape string:". $b;//Output escaped string
echo "<br>";//Wrapping
$c =stripcslashes ($b);//To restore the escaped string
echo "Restore string:". $c;//output restored escape string
?>
</body>
Run Result:
Escape string: \261\340\263\314\314\345abc ' \321\351\315\370
Restore string: Programming body ABC ' Network
Run instructions
1, addslashes function description
String Addslashes (String $str)
Returns a string that is preceded by a backslash for some characters, such as database query statements. These characters are single quotes ('), double quotes ("), backslashes (\) and NUL (NULL characters).
An example of using addslashes () is when you are entering data into a database. For example, insert the name O ' Reilly into the database, which you need to escape.
It is highly recommended that you use the escape function specified by the DBMS (for example, MySQL is mysqli_real_escape_string ()), but if you use a DBMS that does not have an escape function and use \ to escape special characters, you can use this function.
Just to get the data that was inserted into the database, the extra \ does not insert.
When the PHP instruction Magic_quotes_sybase is set to ON, it means that the insert ' will be used ' for escape.
PHP5.4 before the PHP instruction MAGIC_QUOTES_GPC default is on, virtually all get, POST, and cookie data are addslashes (). 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.
Parameters
Str
The character to be escaped.
return value
Returns the escaped character.
2, Stripslashes function description
String Stripslashes (String $str)
References a reference string.
Note:
If the Magic_quotes_sybase entry is turned on, the backslash will be removed, but two backslashes will be replaced with one.
A usage example is to use PHP to detect the opening of MAGIC_QUOTES_GPC configuration items (which are turned on by default before PHP5.4) and you do not need to insert data into a location that needs to be escaped (for example, a database).
For example, you simply output the form data directly.
Parameters
Str
The input string.
return value
Returns a string that removes the escape backslash (\ ' Convert to ', and so on). Double backslash (\ \) is converted to a single backslash (\).
3, Addcslashes function description
String Addcslashes (String $str, String $charlist)
Returns a string that has a backslash in front of the character in the parameter charlist list.
Parameters
Str
The character to be escaped.
Charlist
If the charlist contains characters such as \n,\r, it will be converted in C style, while other non-alphanumeric characters with an ASCII code below 32 and more than 126 are converted to use the octal representation.
When defining a sequence of characters in a charlist parameter, you need to know exactly what characters are within the start and end ranges of your settings.
return value
Returns the escaped character.
4, Stripcslashes function description
String Stripcslashes (String $str)
Returns a string that is inverted. Can be identified like C-language \n,\r, ... Octal and hexadecimal descriptions.
Parameters
Str
A string that needs to be reversed.
return value
Returns a string that is inverted.
Source: http://cakin24.iteye.com/blog/2348605