PHP中转义函数

1.addslashes— 使用反斜线引用字符串

 说明:string addslashes ( string $str )

 返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

   例如:echo addslashes("O'reilly");  //输出:O\'reilly

2.stripslashes - 反引用一个引用字符串

 说明:string stripslashes ( string $str )

   返回一个去除转义反斜线后的字符串(\' 转换为 ' 等等)。双反斜线(\\)被转换为单个反斜线(\)。

    <?php
          $str = "Is your name O\'reilly?";

          // 输出: Is your name O'reilly?
    echo stripslashes($str);
   ?>

    注意:stripslashes() 是非递归的。如果你想要在多维数组中使用该函数,你需要使用递归函数。

<?php
function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}

// 范例
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);

// 输出
print_r($array);
?>

以上例程会输出:

Array
(
    [0] => f'oo
    [1] => b'ar
    [2] => Array
        (
            [0] => fo'o
            [1] => b'ar
        )

)

 

 

posted on 2015-01-18 08:53  地球人阿秋  阅读(88)  评论(0)    收藏  举报

导航