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.
<?php echo addcslashes('sample[ ]', 'A..z'); ?>
This will produce the following output −