The example in this article describes the usage of addslashes() and stripslashes() in PHP to achieve string escaping and restoration. Share it with everyone for your reference, the details are as follows:
The addslashes() function in PHP returns a string with a backslash added before the predefined characters .
The predefined characters are:
Single quote (')
Double quote (")
Backslash ()
NULL
stripslashes() function removes the backslashes added by the addslashes() function.
Usage examples are as follows:
<?php $str="select * from `book` where bookname='本站'"; echo $str."<br/>";//输出字符串$str echo $astr=addslashes($str);//字符串转义并输出 echo "<br/>"; echo stripslashes($astr);//将转义字符串还原 ?>
The running results are as follows:
select * from `book` where bookname='本站' select * from `book` where bookname=\'本站\' select * from `book` where bookname='本站'
Additional: The difference between the
addslashes() and addcslashes() functions:
These are two functions that are very similar in writing but have different uses. The addslashes() function returns a string with a backslash added before the predefined character, while the addcslashes() function returns a string with a backslash added before the specified character, so the addcslashes() function needs to add additional parameters indicating that a backslash needs to be added. The specific characters of the bar.
For the specific differences and usage of the two, please refer to the relevant article on this site "Analysis of the Differences and Comparisons of php addslashes() and addcslashes() Functions"
I hope this article will be helpful to everyone in PHP programming.
The above introduces the usage examples of addslashes and stripslashes in PHP to realize string escaping and restoration, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.