stripslashes

(PHP 4, PHP 5, PHP 7)

stripslashesSupprime les antislashs d'une chaîne

Description

string stripslashes ( string $str )

Supprime les antislashs d'une chaîne.

Note:

Si magic_quotes_sybase est activée, aucun antislash n'est supprimé, mais deux apostrophes sont remplacées par une seule à la place.

Un exemple d'utilisation de stripslashes() est lorsque la directive PHP magic_quotes_gpc est à on (valeur par défaut avant PHP 5.4) et que vous insérez des données dans une base de données qui requiert la protection des valeurs. Par exemple, si vous affichez simplement et directement des données provenant d'un formulaire HTML.

Liste de paramètres

str

La chaîne d'entrée.

Valeurs de retour

Retourne une chaîne dont les antislashs on été supprimés. \' devient ', etc. Les doubles antislashs (\\) sont réduits à un seul antislash (\).

Exemples

Exemple #1 Exemple avec stripslashes()

<?php
$str 
"Avez-vous l\'oreille dure?";

// Affiche : Avez-vous l'oreille dure?
echo stripslashes($str);
?>

Note:

stripslashes() n'est pas récursif. Si vous voulez appliquer cette fonction à un tableau multidimensionnel, vous devez utiliser une fonction récursive.

Exemple #2 Utilisation de stripslashes() sur un tableau

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

    return 
$value;
}

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

// Affiche
print_r($array);
?>

L'exemple ci-dessus va afficher :

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

)

Voir aussi

add a note add a note

User Contributed Notes 30 notes

up
54
ivijan dot stefan at gmail dot com
3 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":

<?php
function 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
Anonymous
3 years ago
Rather use str_replace than explode/implode for your purpose.
up
3
JAB Creations
10 years ago
When writing to a flatfile such as an HTML page you'll notice slashes being inserted. When you write to that page it's interesting how to apply stripslashes...

I replaced this line...
<?php fwrite($file, $_POST['textarea']); ?>

With...
<?php if (get_magic_quotes_gpc()) {fwrite ($file, stripslashes($_POST['textarea']));}?>

You have to directly apply stripslashes to $_POST, $_GET, $_REQUEST, and $_COOKIE.
up
6
StefanoAI
4 years ago
Recursive stripslashes
<?php
if (get_magic_quotes_gpc()) {

    function
stripslashes_array(&$arr) {
        foreach (
$arr as $k => &$v) {
           
$nk = stripslashes($k);
            if (
$nk != $k) {
               
$arr[$nk] = &$v;
                unset(
$arr[$k]);
            }
            if (
is_array($v)) {
               
stripslashes_array($v);
            } else {
               
$arr[$nk] = stripslashes($v);
            }
        }
    }

   
stripslashes_array($_POST);
   
stripslashes_array($_GET);
   
stripslashes_array($_REQUEST);
   
stripslashes_array($_COOKIE);
}
?>
up
5
shredder at technodrome dot com
8 years ago
Hi,

Here are recursive addslashes / stripslashes functions.
given a string - it will simply add / strip slashes
given 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!

<?php

function 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
5
o-zone at zerozone dot it
8 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
2
dragonfly at networkinsight dot net
10 years ago
If you are having trouble with stripslashes() corrupting binary data, try using urlencode() and urldecode() instead.
up
3
alex dot launi at gmail dot com
9 years ago
kibby: I modified the stripslashes_deep() function so that I could use it on NULL values.

function stripslashes_deep($value)
{
    if(isset($value)) {
        $value = is_array($value) ?
            array_map('stripslashes_deep', $value) :
            stripslashes($value);
    }
    return $value;
}
up
2
Kibby
11 years ago
Okay, if using stripslashes_deep, it will definitely replace any NULL to "".  This will affect to coding that depends isset().  Please provide a workaround based on recent note.
up
1
techdesk100
9 years ago
Function which checks if $input has correct slashes,
otherwise adds slashes. For cases when you are not sure the input is not already addslashed.

    public function addslashes_once($input){
        //These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).
        $pattern = array("\\'", "\\\"", "\\\\", "\\0");
        $replace = array("", "", "", "");
        if(preg_match("/[\\\\'\"\\0]/", str_replace($pattern, $replace, $input))){
            return addslashes($input);
        }
        else{
            return $input;
        }
    }
up
1
Yousef Ismaeil Cliprz
4 years ago
If you want to use stripslashes(); function for a string or array you can create a user function

as in example:

<?php

if (!function_exists('strip_slashes'))
{
   
   n .pan>

@autcod v>
/sp;n .pan>?>

Hope it's usefull , O-Zone $i ] =div class="note" id="111426">
stripslashes_array $i ] =
$v)
           
$variable[ $i ] = ] = stripslashes$i strip   
    return
stripslashes_array$i ($_POST] = 41serv class="tally" id="V73820" title="60% li41s?id=73820&page=function.stripslashes&href="#73820" class="name"> dragonfly at networkinsight dot net
4 years ago
If you are having trouble with2-22 06:48hes() corrupting binary data, try using urlencode() and urldecode() 41serv strip slashes
given an array - it will rectripislasdnualllslashesMySQLt de     &nb="keyword">, ", " {(e     rn lss="note" id="nd bm79976"pislouble quclerraby">(e necual/>
>] =div class="note" id="111426"br />$enan-?id=cExemct_tonmyrql(nn class="keyword">] = <0trip_slashes_recursive = myrql_ de ($ de ] = (nn class="keyword">] =
n(n class="keyword">] = ] = '\$row = myrql_fetch_row(& de resan>nn class="keyword">] =
l\$row>]);
    &n> ]);
&nan class="keyword">)) {
    < class="keyword">] =
] = '\$row = myrql_fetch_objmct(& de resan>nn class="keyword">] =
l\$row>]);
    &n> ]);
&nan class="keyword">)) {
)) {
] =
<0trip_slord">)) {
))h (\) and NUL (the Nction l\$resan>>] =div class="note" id="111426" (the Ncass="deecolumns as] =
] =
p(s0BB">f("bsp; rn l: %s",bsp; rn l">(
-?id=ecolumnnbslass="votes"> resan>-?id=ecolumnsp;   }
    }
> trip_slord">)) {
up
<07802usiv class="tally" id="V111426" title="54%07802p?id=111426&page=function.stripslashes&vote=down" title="Vote down!" class="usernotes-voted">down
07802usiv class="tally" id="V111426" title="54%07802p?id=111426&page=function.stripslashe href="#111426" class="name"> Yousef Ismaeil Cliprz
4 years ago 2094erv class="tally" id="V73820" title="60% l2094?id=73820&page=function.stripslashes&href="#73820" class="name"> dragonfly at networkinsight dot net
4 years ago
If you are having trouble with1-02 an s="default">upting binary data, try using urlencode() and urldecode()2094ervit's interesting how to apply stripslashes...( >up
down
47userv class="tally" id="V73820" title="60% 947us?id=90788&page=function.stripslashes&href="#90788" class="name"> shredder at technodrome dot com
4 years agoimavicmss="texts="teza" id="Hcom73820">
<947userns="html"> If you need to remove all slas11-23 05:0 string, here's a quick hack:

& 47uservit's interesting how to apply stripslashes...'strip_slashes'$k $i )
     }
}
));
    return
stripslashes($i     }(
)
     }
}
    return strilashes($i     } if (!ny escapegura

rd">()
     }
}
striInoteaclaz">s! Who'sord"sayet is id=oing ore   &?lashes
);
    }
}

Hope it's usefull , O-Zone$i ?>
?>
?>
down
58userv class="tally" id="V73820" title="60% l58us?id=73820&page=function.stripslashes&href="#73820" class="name"> dragonfly at networkinsight dot net
4 years ago
If you are having trouble with6-20 04:15"default">upting binary data, try using urlencode() and urldecode()58uservexample:

<?phpYbr />
deletault">stripallslasheny t9687" />stripal(ents">n class="keyword">] =div class="note" id="111426" (the Ncass="deean>
] = strialue)hes($value);] =
] = strie" id="111426">
stripal(e
ord">] = strie" id="111426">
ord">] = strie" id="111426">
ord">] = strie" id="111426"an class="keyword">)) {
          < class="keyword">] =
] = strie" id="111426">
ord">] = strie" id="111426"an class="keyword">)) {
     }e" id="111426">
ord">] =
anamp;page=function.stripslashes&vote=up" title="Vote up!" c498usernotes-voteu">up
down
498userv class="tally" id="V73820" title="60% 498us?id=73820&page=function.stripslashes&href="#73820" class="name"> dragonfly at networkinsight dot net
4 years ago
If you are having trouble w5hes()0 func5"default">u2ting binary data, try using urlencode() and urldecode(498uservexample:

<?php

deal
('strip_slaa'$k $i $string,$string< d">)
     }
}
   n>striaceashes$string< d">)
     }
}
   n>stri'lashes$string< d">)
     }
}
   n>striaslashes up
4555"usv class="tally" id="V73820" title="60% 4555"?id=73820&page=function.stripslashes&vote=down" title="Vote down!" class="usernotes-voted">down
4555"usv class="tally" id="V73820" title="60% 4555"?id=73820&page=function.stripslashes&href="#73820" class="name"> dragonfly at networkinsight dot net
4 years ago
If you are having trouble w4-las10 an 5="default">u3ting binary data, try using urlencode() and urldecode(4555"usvexample:

<?ptushois_abes="kg cl" {atviclass="notsp; get rids="k { hs=eld,et ode>sp; every>  stripiup
<09189usiv class="tally" id="V111426" title="54%09189p?id=111426&page=function.stripslashes&vote=down" title="Vote down!" class="usernotes-voted">down
09189usiv class="tally" id="V111426" title="54%09189p?id=111426&page=function.stripslashe href="#111426" class="name"> Yousef Ismaeil Cliprz
4 years ago
If you want to use stripsla2uc6-26deep(ect slashes5tring or array you can create a user function


<?Attempdiv i /> se {
n r"ect1 ", boteins5.3.6 ser />mp;page=function.stripslashes&vote=up" title="Vote up!" clas10133sernotes-voteu">up
down
0133seriv class="tally" id="V111426" title="54%0133sp?id=111426&page=function.stripslashe href="#111426" class="name"> Yousef Ismaeil Cliprz
4 years ago
If you want to use stripsla0ashe10 12:39ct slashes7tring or array you can create a user function


<?p'm :n>'strip_slashes'$variable (}
fu_xists   n>?>
)fpan class=" a lot of&n*
&aan style
up
down
9183erv class="tally" id="V73820" title="60% 99183?id=90788&page=function.stripslashes&href="#90788" class="name"> shredder at technodrome dot com
4 years ago
If you want to use stripsla0anput1 an 04ct slashes7tring or array you can create a user function

given an array - it will rectri clasw/><="ksp; ('strip_slashes'   n> <" rn )
     } sp;  slashes_array
$variable <" rn $i stripslashes_array$variable ?lass="default">$i $strin rn $i }
$variable :p; while(
stripslashes_array$variable ord">] }iv id="Vu66081"> ord">] rd">()
     }
}
lass="default">$i $strin rn $i }
$variable ord">] rd">(
)
     }
}
lass="default">$i $strin rn $i }
);
ord">] rd">(
)
     }
}
lass="default">$i $strin rn $i }
);
down
2524erv class="tally" id="V73820" title="60% 92524?id=90788&page=function.stripslashes&href="#90788" class="name"> shredder at technodrome dot com
4 years ago
If you need to remove all slas07has c5nction for a re's a quick hack:

& 2524erv strip slashes
given an array - it will reci,(
an>(e nd la{atvan csliv becsp; 'strip_slashes'$k )
     
$s nd lass="default">$i }
,$si d">)
     }
}
  &n/ loop ode>' ope it's usefull , O-Zone$i $s nd lass="default">$i }
,$si d">)
     !}
}
,
ord">] = $s nd lass="default">$i }
?>

Hope it's usefull , O-Zone$i     }($i     })) {
))h (\) and NUL (the N> $i down
9785erv class="tally" id="V73820" title="60% i9785?id=82831&page=function.stripslashes&href="#82831" class="name"> techdesk100
4 years ago
If you need to remove all slas03-23 07:s inn for a re's a quick hack:

& 9785ervexample:

<?A nbsp; &bsp;na{atv hois_abessafe'strip_slashes')
     ?>
&),sp; &nbo [1] =>
=
,}
lass="default">$i
up
down
2121erv class="tally" id="V73820" title="60% i2121?id=82831&page=function.stripslashes&href="#82831" class="name"> techdesk100
4 years ago
If you need to remove all sl8s03-27 10:0 string, he9e's a quick hack:

& 2121erv strip slashes
given an array - it will rectripislng ore eyword"=asdnung"chss="defau&nbon   &ninss="rdbarrays. lfid">(
'strip_slashes'$k $string< d">)
     ?>

Hope it's usefull , O-Zone
<"kean class=" a lot of&n*
&his proord">)) {
up
3787"usv class="tally" id="V73820" title="60% 3787"?id=82831&page=function.stripslashes&vote=down" title="Vote down!" class="usernotes-voted">down
3787"usv class="tally" id="V73820" title="60% 3787"?id=82831&page=function.stripslashes&href="#82831" class="name"> techdesk1004 years ago samura <="tef" id="Hcom79976"> down
327serv class="tally" id="V73820" title="60% 9327s?id=90788&page=function.stripslashes&href="#90788" class="name"> shredder at technodrome dot com
4 years ago roszka<="tepl id="Hcom79976">
If you need to remove all slas08-31 an 00ion for a re's a quick hack:

& 327servit's interesting how to apply stripslashes...( -p; &nb-('strip_slashe d">)
     class="default">?>
}
'strip_slashe d">)
     class="default">?>
?>

Hope it's usefull , O-Zone}
do v,y(e m& o$_GE['eyword"'])eonly manus,eicl>(e m&sy-not9isldis9687d (cnot 2(e assckeiclbodiv>(e (&sy-not9"notphp687d (cnot 3>);
< class="string">'strip_slashes'$k )
     }}
}
?>

Hope it's usefull , O-class="deget_(&
,$1 d">)
     }}
}
   n> )
     }
}
$i $ini_get">?>

Hope it's usefull , O-class="de(&
sy-notpan class=" a lot of&n*
&aan style )
     }} empdnbo [1] =>
Zone,?>
)
     }}
}
,< ] = ,an class="keyword">)) {
  < class="keyword">] =
] = ,an class="keyword">))an class="keyword">))hspan>do v,y$k 6214"usv class="tally" id="V73820" title="60% 6214"?id=90788&page=function.stripslashes&href="#90788" class="name"> shredder at technodrome dot com
4 years ago
If you need to remove all sl6hes(21 02:1 string, he11e's a quick hack:

&6214"usvit's interesting how to apply stripslashes...r />sp; ng(0) "" ;
< class="string">'strip_slashes')
     }
}
an class=" a lot of&n*
&his pro
}

Hope it's usefull , O-Zone$i
}
)
     }
}
?>

Hope it's usefull , O-Zone$i }

Hope it's usefull , O-Zone$i up
89819usv class="tally" id="V73820" title="60% i9819?id=82831&page=function.stripslashes&vote=down" title="Vote down!" class="usernotes-voted">down
9819usv class="tally" id="V73820" title="60% i9819?id=82831&page=function.stripslashes&href="#82831" class="name"> techdesk100 1402usv class="tally" id="V73820" title="60% i1402?id=82831&page=function.stripslashes&href="#82831" class="name"> techdesk100