(PHP 4, PHP 5, PHP 7, PHP 8)
stripslashes — Quita las barras de un string con comillas escapadas
Quita las barras de un string con comillas escapadas.
stripslashes() se puede utilizar si no está insertando estos datos en un lugar (como una base de datos) que requiere escapar. Por ejemplo, si simplemente está imprimiendo datos directamente desde un formulario HTML.
strEl string de entrada.
Devuelve un string con las barras invertidas retiradas.
(\' se convierte en ' y así sucesivamente.)
Barras invertidas dobles (\\) se convierten en una
sencilla (\).
Ejemplo #1 Un ejemplo de stripslashes()
<?php
$str = "Is your name O\'reilly?";
// Salida: Is your name O'reilly?
echo stripslashes($str);
?>Nota:
stripslashes() no es recursiva. Si se desea aplicar esta función a un array multi-dimensional, se necesita utilizar una función recursiva.
Ejemplo #2 Utilizando stripslashes() en un array
<?php
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// Ejemplo
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);
// Salida
print_r($array);
?>El ejemplo anterior mostrará:
Array ( [0] => f'oo [1] => b'ar [2] => Array ( [0] => fo'o [1] => b'ar ) )
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.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 ;
}
?>A replacement that should be safe on utf-8 strings.
<?php
preg_replace(array('/\x5C(?!\x5C)/u', '/\x5C\x5C/u'), array('','\\'), $s);
?>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-Zonein 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.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!