- <?php
- $str = addslashes('Shanghai is the "biggest" city in China.');
- echo($str);
- //Shanghai is the \"biggest\" city in China.
- ?>
定义和用法
addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。
预定义字符是:
单引号(')
双引号(")
反斜杠(\)
NULL
提示:该函数可用于为存储在数据库中的字符串以及数据库查询语句准备字符串。
addslashes
(PHP 4, PHP 5, PHP 7)
addslashes — 使用反斜线引用字符串
说明
string addslashes ( string $str )
返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。
这些字符是单引号(')、双引号(")、反斜线(\)与 NUL( NULL 字符)。
参数
str 要转义的字符。
返回值
返回转义后的字符。
示例:
- <?php
- $str = "Is your name O'reilly?" ;
- // 输出: Is your name O\'reilly?
- echo addslashes ( $str );