array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.addslashes.php', 1 => 'addslashes', ), 'up' => array ( 0 => 'ref.strings.php', 1 => '字符串 函数', ), 'prev' => array ( 0 => 'function.addcslashes.php', 1 => 'addcslashes', ), 'next' => array ( 0 => 'function.bin2hex.php', 1 => 'bin2hex', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/strings/functions/addslashes.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
addslashes — 使用反斜线引用字符串
返回需要在转义字符之前添加反斜线的字符串。这些字符是:
'
)"
)\
)addslashes() 的一个用法就是转义由 PHP 执行字符串中的上述字符:
<?php
$str = "O'Reilly?";
eval("echo '" . addslashes($str) . "';");
?>
有时会错误的使用 addslashes() 来防止 SQL 注入。相反,应该使用数据库特定函数和/或预处理语句。
string
要转义的字符。
返回转义后的字符。
示例 #1 一个 addslashes() 例子
<?php
$str = "Is your name O'Reilly?";
// 输出: Is your name O\'Reilly?
echo addslashes($str);
?>