The addcslashes() function returns a string with backslashes added in front of the specified characters.
The addcslashes() function is case-sensitive.
PHP addcslashes() function has the following syntax.
addcslashes(string,characters)
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to be escaped |
characters | Required. | Characters or range of characters to be escaped |
PHP addcslashes() function returns the escaped string.
Add a backslash in front of the character "W"
<?php
$str = addcslashes("Hello World!","W");
echo($str);
?>
The code above generates the following result.
Add backslashes to certain characters in a string:
<?php
$str = "Welcome to java2s.com!";
echo $str."\n";
echo addcslashes($str,'m')."\n";
echo addcslashes($str,'H')."\n";
?>
The code above generates the following result.
Add backslashes to a range of characters in a string:
<?php
$str = "Welcome to java2s.com!";
echo $str."\n";
echo addcslashes($str,'A..o')."\n";
echo addcslashes($str,'a..o')."\n";
echo addcslashes($str,'a..j');
?>
The code above generates the following result.