PHP addcslashes() Function
Definition
The addcslashes() function returns a string with backslashes added in front of the specified characters.
The addcslashes() function is case-sensitive.
Syntax
PHP addcslashes() function has the following syntax.
addcslashes(string,characters)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to be escaped |
characters | Required. | Characters or range of characters to be escaped |
Return
PHP addcslashes() function returns the escaped string.
Example 1
Add a backslash in front of the character "W"
<?php
$str = addcslashes("Hello World!","W");
echo($str);
?>
The code above generates the following result.
Example 2
Add backslashes to certain characters in a string:
<?php/*from w w w.ja v a 2s. co m*/
$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.
Example 3
Add backslashes to a range of characters in a string:
<?php/*from ww w . ja va 2 s.c om*/
$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.