Usage examples of string escaping and restoration using addslashes and stripslashes in PHP

WBOY
Release: 2016-07-29 09:07:19
Original
1095 people have browsed it

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:

<&#63;php
 $str="select * from `book` where bookname='本站'";
 echo $str."<br/>";//输出字符串$str
 echo $astr=addslashes($str);//字符串转义并输出
 echo "<br/>";
 echo stripslashes($astr);//将转义字符串还原
?>

Copy after login

The running results are as follows:

select * from `book` where bookname='本站'
select * from `book` where bookname=\'本站\'
select * from `book` where bookname='本站'

Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template