Last Updated on July 22, 2021 by Roshan Parihar
PHP addcslashes() Function is used to add a backslash (\) before the specified character of a string. You have to specify a character present in a string to insert a backslash before it.
Syntax of PHP addcslashes() Function
The function two arguments in which both should be a string add a backslash in a given string in PHP.
Description of Parameters
Name | Description |
---|---|
string | Required. Specify the string that you have to use to be escaped or add a backslash before the specified characters. |
characters | Required. It is a character or sequence of characters of a string that you have to specify to escape in a given string. The function adds a backslash (\) before that specified character. |
Let’s see some examples that are useful to understand the use of the PHP addcslashes() Function in PHP.
Examples of PHP addcslashes() Function
Here are some useful examples to learn the function.
Escape Single and Multiple Characters to Add Backslashes
Example 1: Escape single character
When you want to escape a single character in a string, you can use the PHP addcslashes() Function as given in the example below.
1 2 3 4 5 6 7 8 9 10 |
<?php //Define a string in PHP $myStr = "TutorialDeep"; //Add backslash to the specified characters using addcslashes() $myNewStr = addcslashes($myStr, "D"); //Print result echo $myNewStr ?> |
Output
The output shows that the functions escape a single character of the given string in PHP. The example adds the second argument as a single character of the string. You can also store it to a variable and pass that variable as the second argument of the function
Example 2: Escape more than one characters
Similarly, if want to escape more specified characters in a given string, you have to specify the sequence of characters as given below.
1 2 3 4 5 6 7 8 9 10 |
<?php //Define a string in PHP $myStr = "TutorialDeep"; //Add backslash to the specified characters using addcslashes() $myNewStr = addcslashes($myStr, "De"); //Print result echo $myNewStr ?> |
Output
The above example escapes two specified characters in a string using the function. Likewise the above example, you can also specify more characters of a string to escape or add backslash.
Character are Case-sensitive to Specify in PHP addcslashes() Function
Example 3: Shows the character is case-sensitive
The specified character of a string in the PHP addcslashes() Function is case-sensitive. So, be careful with the capital and small letters of characters of a string in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //Define a string in PHP $myStr = "Welcome to TutorialDeep"; //Escape characters with case-sensitivity $myNewStr1 = addcslashes($myStr, "e"); $myNewStr2 = addcslashes($myStr, "T"); //Print result echo $myNewStr1."<br>"; echo $myNewStr2; ?> |
Output
Welcome to \TutorialDeep
The first result escaped the small ‘t’ character and the second result escaped the capital ‘T’ character in a string.
Escape Range of Characters
Example 4: Escape Range of Character in a String
In addition to the above methods, you can also escape the range of characters in a string. To escape the range of characters, you have to specify the range of characters as given in the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php //Define a string in PHP $myStr = "Welcome to TutorialDeep"; //Escape characters with case-sensitivity $myNewStr1 = addcslashes($myStr, "a..f"); $myNewStr2 = addcslashes($myStr, "A..F"); $myNewStr3 = addcslashes($myStr, "a..z"); //Print result echo $myNewStr1."<br>"; echo $myNewStr2."<br>"; echo $myNewStr3; ?> |
Output
Welcome to Tutorial\Deep
W\e\l\c\o\m\e \t\o T\u\t\o\r\i\a\lD\e\e\p
The first line result escaped the characters from the small letter ‘a to f’. The second line result escaped the characters from capital ‘A to f’. The third line result escaped all the small characters as it is specified as ‘a to z’.
You May Also Like to Read