Home > Backend Development > PHP Tutorial > PHP中addslashes()和stripslashes()实现字符串转义和还原用法实例_php技巧

PHP中addslashes()和stripslashes()实现字符串转义和还原用法实例_php技巧

WBOY
Release: 2016-05-16 20:00:32
Original
1035 people have browsed it

本文实例讲述了PHP中addslashes()和stripslashes()实现字符串转义和还原用法。分享给大家供大家参考,具体如下:

PHP中addslashes() 函数返回在预定义字符之前添加反斜杠的字符串

预定义字符是:

单引号(')
双引号(")
反斜杠(\)
NULL

stripslashes() 函数删除由 addslashes() 函数添加的反斜杠

用法示例如下:

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

Copy after login

运行结果如下:

select * from `book` where bookname='脚本之家'
select * from `book` where bookname=\'脚本之家\'
select * from `book` where bookname='脚本之家'

Copy after login

补充:

addslashes()与addcslashes()函数的区别:

这是两个写法非常相近但是使用有所区别的函数,addslashes() 函数返回在预定义字符之前添加反斜杠的字符串,而addcslashes() 函数返回在指定字符前添加反斜杠的字符串,因此addcslashes() 函数需要添加附加参数说明需要添加反斜杠的具体字符。

二者具体区别与用法可参考本站相关文章《解析php addslashes()与addcslashes()函数的区别和比较

希望本文所述对大家PHP程序设计有所帮助。

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