This article describes the examples of addslashes() and stripslashes in PHP () implements string escape and restoration usage. 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 predefined characters.
The predefined characters are:
Single quote (')
Double quotes (")
backslash()
NULL
The 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='帮客之家'
Supplement:
The difference between 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 characters, while the addcslashes() function returns a string with a backslash added before the specified character. string, so the addcslashes() function needs to add additional parameters indicating the specific characters that need to be added with backslashes.
For the specific differences and usage between the two, please refer to the relevant article on this site "Analysis of the Difference and Comparison of PHP addslashes() and addcslashes() Functions"
I hope this article will be helpful to everyone in PHP programming.