PHP addcslashes()Functions
Example
Add a backslash before the character "W":
$str = addcslashes("Hello World!","W");
echo($str);
Run Example »
Definition and Usage
The addcslashes() function returns the string with backslashes added before the specified characters.
Note:The addcslashes() function is case-sensitive.
Note:Be careful when applying addcslashes() to 0 (NULL), r (carriage return), n (newline), t (tab), f (form feed), and v (vertical tab). In PHP, \0, \r, \n, \t, \f, and \v are predefined escape sequences.
Syntax
addcslashes(string,characters)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to escape. |
| characters | Required. Specifies the characters or character ranges to escape. |
Technical Details
| Return Value: | Returns the escaped string. |
|---|---|
| PHP Version: | 4+ |
More Examples
Example 1
Add backslashes to specific characters in a string:
<?php
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,'m')."<br>";
echo addcslashes($str,'H')."<br>";
?>
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,'m')."<br>";
echo addcslashes($str,'H')."<br>";
?>
Run Example »
Example 2
Add backslashes to characters in a range within a string:
<?php
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,'A..Z')."<br>";
echo addcslashes($str,'a..z')."<br>";
echo addcslashes($str,'a..g');
?>
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,'A..Z')."<br>";
echo addcslashes($str,'a..z')."<br>";
echo addcslashes($str,'a..g');
?>
Run Example »
PHP String Reference Manual Other Extensions