PHP stripslashes() Function

PHP’s stripslashes() removes backslash escaping and returns a new string. One call recovers a string escaped once with addslashes(). Check how a file path or user-entered value was stored before applying the function.

What does stripslashes() restore?

stripslashes(string $string): string

The function takes one string. It restores escaped quotes and turns each pair of backslashes into one backslash. It converts \0, a backslash followed by the digit zero, to a single NUL byte with a numeric value of zero. The forward slash / stays as it is.

PHP manual for stripslashes()

Compare a round trip with a damaged path

Save the example as stripslashes-demo.php and run php stripslashes-demo.php in a terminal with PHP installed. The first line shows the restored quote. The second line’s true confirms that the string containing a NUL byte was restored byte for byte.

<?php
echo stripslashes("PHP\\'s sample"), PHP_EOL;

$original = "PHP's \"sample\" | A\0B";
$restored = stripslashes(addslashes($original));
var_dump($restored === $original);

$path = 'C:\new\notes';
echo $path, PHP_EOL;
echo stripslashes($path), PHP_EOL;

$sequence = '\\n';
echo bin2hex(stripslashes($sequence)), PHP_EOL;
echo bin2hex(stripcslashes($sequence)), PHP_EOL;
PHP's sample
bool(true)
C:\new\notes
C:newnotes
6e
0a

The third line shows an ordinary Windows path. Applying stripslashes() directly to it removes the directory separators, as the fourth line shows. The final lines display 6e for the letter n and 0a for a newline. These are the results of passing the same two-character sequence \n to the two functions.

Choose the decoder that matches the stored format

Use stripslashes() when you know that the stored string was passed through addslashes(). A value read from a database can contain meaningful backslashes, such as the separators in a path. For an array, transform only the string fields you know were escaped. For C-style escapes such as \n and hexadecimal sequences, use stripcslashes().