PHP function stripslashes() – Un-quotes a quoted string

The stripslashes() function is used to unquote a string that is quoted by using the addslashes() function. This function was introduced in PHP3. The stripslashes() function is the opposite of addslashes(): it removes one set of \-escapes from a string.

Syntax:

stripslashes(string $string): string

To remove backslashes and convert C-style escape sequences to their literal values:

stripcslashes() 

To add slashes to a string:

addslashes() 

Parameters

Parameter Description
string The string to be unescaped.

Return Values

Returns a string with backslashes stripped off. (\’ becomes ‘ and so on.) Double backslashes (\\) are made into a single backslash (\).

Examples

Example 1:

<?php
$str = “Are you Jack?”; // Outputs: Are you Jack? echo stripslashes($str); ?>

Example 2:

$string = "I'm a lumberjack and I'm okay!";
$a = addslashes($string);
// string is now "I\'m a lumberjack and I\'m okay!"

$b = stripslashes($a);
// string is now "I'm a lumberjack and I'm okay!"

Example 3: Remove slashes from data retrieved from a database

// database connection code omitted for brevity 

$author = 'Sam Clemens'; 
// query a db 
$query = "SELECT quote FROM aphorisms WHERE author like "$author'"; 
$result = mysql_query ($query); 

// write out the results of the query 
if (0 == mysql_num_rows ($result)) {
   die ("Sorry, no witticisms from $author for you today!"); 
} 

while ($temp = mysql_fetch_row ($result)) {
   echo stripslashes ($temp[0]), "\n\n"; 
}

Final Thoughts

stripslashes() removes the backslashes from any escape sequence that it encounters in a string. An escape sequence starts with a backslash and is followed by one or more characters. This function is often used to strip backslashes from data retrieved from a database or to clean up data submitted by an HTML form.

Related Post