Content-Length: 79929 | pFad | https://www.php.net/manual/pt_BR/function.stripslashes.php

PHP: stripslashes - Manual
PHPerKaigi 2025

stripslashes

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

stripslashesDesfaz os escapes de uma string escapada

Descrição

stripslashes(string $string): string

Remove barras invertidas de uma string.

stripslashes() pode ser utilizada quando você não esta inserindo estes dados em um lugar (como um banco de dados) que precise de escapar. Por exemplo, se você esta usando estes dados para gerar um formulário HTML.

Parâmetros

string

A string de entrada.

Valor Retornado

Retorna uma string com as barras invertidas retiradas. (\' se torna ' e assim por diante.) Duas barras invertidas (\\) se tornam uma (\).

Exemplos

Exemplo #1 Exemplo stripslashes()

<?php
$str
= "Seu nome é O\'reilly?";

// Mostra: Seu nome é O'reilly?
echo stripslashes($str);
?>

Nota:

stripslashes() não é recursiva. Se você quer aplicar esta função a um array multi-dimensional, você precisa usar uma função recursiva.

Exemplo #2 Usando stripslashes() em um array

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

return
$value;
}

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

// Output
print_r($array);
?>

O exemplo acima produzirá:

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

)

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 6 notes

up
71
ivijan dot stefan at gmail dot com
10 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
shredder at technodrome dot com
15 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
1
Tom Worster
15 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
15 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








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.php.net/manual/pt_BR/function.stripslashes.php

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy