stripslashes() removes backslashes before quoted characters and backslashes. Use it only when the input format actually contains those escape slashes.
$text = "O\\'Reilly";
echostripslashes($text);
The PHP stripslashes() function is used to remove backslashes from a string that were already present or previously added using addslashes() or manually escaped. Use this only when the input format is known to contain slash escapes. Modern database APIs and ordinary HTML form input should not require these slashes to be removed.
Syntax
string stripslashes ( string $string )
$string: The input string where the backslashes need to be removed. Return Value: A string with slash escapes removed; this can also strip a backslash before ordinary characters, so apply it only to data known to use slash escaping.
Basic Example of stripslashes()
In this simple example, we remove the backslashes that were added to escape quotes.
$str = "John\'s book";
echostripslashes($str); // Backslashes are removed before single quotes
Output:
John's book
Example with Double Quotes and Backslashes
In this example, we demonstrate how stripslashes() removes backslashes from both single and double quotes.
$str = "He said, \"It\'s a test!\"";
echostripslashes($str); // Removes backslashes before both single and double quotes
Output:
He said, "It's a test!"
Example with Escaped Backslashes
This example shows how stripslashes() handles a string that has escaped backslashes.
$str = "C:\\\\Program Files\\\\";
echostripslashes($str); // Removes the extra backslashes
Output:
C:\Program Files\
Using stripslashes() to Process Form Input
When handling user inputs from forms where addslashes() has been used, you can use stripslashes() to clean the input data.
$user_input = "O\'Reilly";
$clean_input = stripslashes($user_input);
echo $clean_input; // Removes backslashes added to escape single quotes
Output:
O'Reilly
Conclusion
The PHP stripslashes() function is useful when processing strings that have been escaped with backslashes. It ensures that data is displayed or used in its original format by removing unnecessary backslashes. This function is commonly used when retrieving data from databases or processing form inputs that involve special characters.
← String Functionsaddslashes() →Remove HTML tags →
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.