The addcslashes function can be used. Below is the syntax of the function −
string addcslashes ( string $str, string $charlist )
This function returns a string with backslashes that appear before the characters. Below is a demonstration of the function.
Example
<?php echo addcslashes('sample[ ]', 'A..z'); ?>
Output
This will produce the following output −
\s\a\m\p\l\e\[ \]