In this article, you will learn;
- How to add backslashes in front of the specified characters in the string.
The PHP addcslashes() function returns a string by adding backslashes at the beginning of the specified character in the string.
PHP pre-defined escape sequence of characters is;
- \0 – NULL
- r – Carriae return
- n – newline
- f – form feed
- t – tab
- v – verticle tab
Note: You must consider and take care of the above-mentioned predefined escape characters in PHP while using the addcslahes() function.
Note: The addcslashes() method shows case insensitive behavior.
What is the syntax of the ADDCSLASHES() function in PHP?
addcslashes(string,characters)
Parameter | Description |
---|---|
string | The string to be escaped – Required |
characters | Character to be escaped. Also can be range of characters – Required |
Example of the ADDCSLASHES() function
Example 1. In this example, we add a backslash before the character P.
<?php
$str = addcslashes("Hello PHP!","P");
echo($str);
?>
Example 1. In this example, we add backslashes before the letters H and S.
<?php
$str = "Home Sweet Home";
echo $str."<br>";
echo addcslashes($str,'S')."<br>";
echo addcslashes($str,'H')."<br>";
?>
Example 1. In this example, we add backslashes to the all the letters within the range.
<?php
$str = "Home Sweet Home!";
echo $str."<br>";
echo addcslashes($str,'A..Z')."<br>";
echo addcslashes($str,'a..z')."<br>";
echo addcslashes($str,'a..h');
?>