The PHP stripslashes() function is used to remove backslashes from a string that were already present or previously added using addslashes() or manually escaped. It is useful when retrieving data from databases or handling form inputs where backslashes have been added for escaping purposes.
Syntax
string stripslashes ( string $string )
$string: The input string where the backslashes need to be removed. Return Value: A string with all backslashes removed.
Basic Example of stripslashes()
In this simple example, we remove the backslashes that were added to escape quotes.
$str = "John\'s book";
echo stripslashes($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!\"";
echo stripslashes($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\\\\";
echo stripslashes($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 → ← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com
✖
We use cookies to improve your browsing experience. . Learn more