stripslashes

(PHP 4, PHP 5, PHP 7, PHP 8)

stripslashes反引用一个引用字符串

说明

stripslashes(string $string): string

反引用一个引用字符串。

如果不需要将数据插入到一个需要转义的位置(例如数据库)则可以使用 stripslashes()。例如,直接从 HTML 表单输出数据。

参数

string

输入字符串。

返回值

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

示例

示例 #1 stripslashes() 示例

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

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

注意:

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

示例 #2 对数组使用 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
        )

)

参见

添加备注

用户贡献的备注 6 notes

up
71
ivijan dot stefan at gmail dot com
11 years ago
Sometimes for some reason is happens that PHP or Javascript or some naughty insert a lot of  backslash. Ordinary function does not notice that. Therefore, it is necessary that the bit "inflate":<?phpfunction removeslashes($string){    $string=implode("",explode("\\",$string));    return stripslashes(trim($string));}/* Example */$text="My dog don\\\\\\\\\\\\\\\\'t like the postman!";echo removeslashes($text);?>RESULT: My dog don't like the postman!This flick has served me wery well, because I had this problem before.
up
6
shredder at technodrome dot com
16 years ago
Hi, Here are recursive addslashes / stripslashes functions.given a string - it will simply add / strip slashesgiven an array - it will recursively add / strip slashes from the array and all of it subarrays. if the value is not a string or array - it will remain unmodified!<?phpfunction add_slashes_recursive( $variable ){    if ( is_string( $variable ) )        return addslashes( $variable ) ;    elseif ( is_array( $variable ) )        foreach( $variable as $i => $value )            $variable[ $i ] = add_slashes_recursive( $value ) ;    return $variable ;}function strip_slashes_recursive( $variable ){    if ( is_string( $variable ) )        return stripslashes( $variable ) ;    if ( is_array( $variable ) )        foreach( $variable as $i => $value )            $variable[ $i ] = strip_slashes_recursive( $value ) ;        return $variable ; }?>
up
1
Tom Worster
16 years ago
A replacement that should be safe on utf-8 strings.<?php  preg_replace(array('/\x5C(?!\x5C)/u', '/\x5C\x5C/u'), array('','\\'), $s);?>
up
2
o-zone at zerozone dot it
16 years ago
If you need to remove all slashes from a string, here's a quick hack:

<?php
function stripallslashes($string) {
    while(strchr($string,'\\')) {
        $string = stripslashes($string);
    }
}
?>

Hope it's usefull , O-Zone
up
1
stoic
18 years ago
in response to crab dot crab at gmail dot com:$value need not be passed by reference. The 'stripped' value is returned. The passed value is not altered.
up
1
hash at samurai dot fm
21 years ago
Might I warn readers that they should be vary careful with the use of stripslashes on Japanese text. The shift_jis character set includes a number of two-byte code charcters that contain the hex-value 0x5c (backslash) which will get stripped by this function thus garbling those characters.What a nightmare!
To Top