PHP stripslashes Function | Remove Backslashes From String

PHP stripslashes is an inbuilt function in PHP. It removes backslashes from a string. You can use it recover the original string after escaping it using PHP addslashes Function. In this article, we will discuss the PHP stripslashes Function.

Syntax

stripslashes($string)

Parameters

The function expects only one parameter as shown in the syntax.

  • $string: The input string from which you want to remove slashes must be passed as the parameter.

Return Value

The stripslashes function returns the string after removing backslashes and un-quoting it (\’ becomes ‘). Also, it converts double backslashes (\\) to single backslash (\).

PHP stripslashes Function

Examples

Let’s consider a few examples of using the PHP stripslashes.

Example 1: Simple String

<?php

	$testString = 'Bringing \"Knowledge\" Together';
	$strippedString = stripslashes($testString);

?>

In the above example, we pass an escaped string to the stripslashes function. The function removes all the backslashes and returns the un-quoted string.

OUTPUT:
Bringing "Knowledge" Together

Example 2: Multi-Dimensional Array

This function is not a recursive function and works only on strings. However, you can remove slashes from a multi-dimensional array using it with PHP array_map Function.

<?php

	function stripslashes_recursive($object) {
		return is_array($object) ? array_map('stripslashes_recursive', $object) : stripslashes($object);
	}

	$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
	$strippedArray = stripslashes_recursive($array);

?>

The final array will become:

Array
(
    [0] => f'oo
    [1] => b'ar
    [2] => Array
        (
            [0] => fo'o
            [1] => b'ar
        )
)

Conclusion

In conclusion, we discussed the PHP stripslashes Function. You can read more about it on PHP Official Documentation. Additionally, you can learn more about PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *