Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String stripslashes() Function



The PHP String stripslashes() function is used to remove backslashes in a string. It can be used to clean up data retrieved from a database or from an HTML form. This function basically accepts only one parameter which is used to specify the string to check.

This function can be used if you are not inserting this data into a location that needs escaping. For example, imagine you are just exporting data from an HTML form.

Syntax

Below is the syntax of the PHP String stripslashes() function −

string stripslashes ( string $string )

Parameters

This function accepts $string parameter which is a string to check.

Return Value

The stripslashes() function returns a string with backslashes stripped off.

PHP Version

First introduced in core PHP 4, the stripslashes() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP String stripslashes() function to show how it removes backslashes.

<?php
   // Basic Usage of stripslashes()
   // String with escaped single quote
   $inputString = "Hello, it\ is a sunny day!"; 
   $outputString = stripslashes($inputString);

   //  Print the result
   echo "Original String: $inputString\n";
   echo "Unquoted String: $outputString\n";
?>

Output

Here is the outcome of the following code −

Original String: Hello, it\ is a sunny day!
Unquoted String: Hello, it's a sunny day!

Example 2

In the below PHP code we will use the stripslashes() function and process data submitted from an HTML form where quotes are escaped.

<?php
   // Using stripslashes with HTML form data
   // Simulated form input with escaped single quote
   $formInput = "I\'m learning PHP!"; 
   $funcInput = stripslashes($formInput);

   // Print the results
   echo "Form Input : $formInput";
   echo "\n Processed Input for output $funcInput \n";
?> 

Output

This will generate the below output −

Form Input : I\'m learning PHP!
 Processed Input for output I'm learning PHP! 

Example 3

Now the below code uses the stripslashes() function on JSON data with escaped characters.

<?php
   // Handling JSON data with stripslashes
   // JSON string with escaped characters
   $jsonInput = "{\"name\": \"O\'Connor\", \"age\": 30}"; 
   $decodedJson = stripslashes($jsonInput);

   // Print the results
   echo "Original JSON: $jsonInput\n";
   echo "Decoded JSON: $decodedJson\n";
?> 

Output

This will create the below output −

Original JSON: {"name": "O\'Connor", "age": 30}
Decoded JSON: {"name": "O'Connor", "age": 30}

Example 4

In the following example, we are using the stripslashes() function to remove escape characters from dynamically created strings with backslashes.

<?php  
   // stripslashes with dynamically generated strings
   // String with escaped backslashes
   $dynamicString = "The file path is C:\\Users\\Amit\\Documents\\Project\\file.txt"; 
   $cleanedString = stripslashes($dynamicString);

   // Print the results
   echo "Dynamic String: $dynamicString\n";
   echo "Cleaned String: $cleanedString\n";
?> 

Output

Following is the output of the above code −

Dynamic String: The file path is C:\Users\John\Documents\Project\file.txt
Cleaned String: The file path is C:UsersJohnDocumentsProjectfile.txt
php_function_reference.htm
Advertisements