PHP Function Reference

PHP stripslashes() Function



The PHP stripslashes() function removes backslashes added by the addslashes() function.

This function is useful when inserting the data into a place (such as a database) that does not require escaping. For example, simply outputting the data straight from an HTML form.

Note: stripslashes() is not recursive. To apply this function to a multi-dimensional array, a recursive function can be used.

Syntax

stripslashes(string)

Parameters

string Required. Specify the input string.

Return Value

Returns a string with backslashes stripped off.

Example:

The example below shows the usage of stripslashes() function.

<?php
$str1 = 'John loves \"Programming\"';
$str2 = "John\'s A/C number is \'1234\'";

//returns: John loves "Programming"
echo stripslashes($str1)."\n";

//returns: John's A/C number is '1234'
echo stripslashes($str2);
?>

The output of the above code will be:

John loves "Programming"
John's A/C number is '1234'

Example:

Consider one more example where this function is used recursively on a multi-dimensional array.

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

  return $value;
}

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

//before removing backslashes
print_r($Arr);

//removing backslashes recursively
$Arr = stripslashes_deep($Arr);

echo "\n";
//displaying the result
print_r($Arr);
?>

The output of the above code will be:

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

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

❮ PHP String Reference