PHP addcslashes() Function
The PHP addcslashes() function returns a string with backslashes in front of characters that are listed in characters parameter.
Syntax
addcslashes(string, characters)
Parameters
string |
Required. Specify the string to be escaped. |
characters |
Required. Specify the characters or range of characters to be escaped. |
Note: Be careful , using this function on 0, a, b, f, n, r, t and v. They will be converted to \0 (NULL), \a (alert), \b (backspace), \f (form feed), \n (newline), \r (carriage return), \t (tab) and \v (vertical tab), all of which are predefined escape sequences in C.
Note: If characters contains characters \n, \r etc., they are converted in C-like style, while other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 converted to octal representation. Therefore when defining a sequence of characters for characters argument, make sure that characters should come between the characters defined in the range.
Return Value
Returns the escaped string.
Example:
The example below shows the usage of addcslashes() function.
<?php $str = 'John loves Programming'; //adds backslashes in front of 'o' echo addcslashes($str, 'o')."\n"; //adds backslashes in front of 'o' and 'm' echo addcslashes($str, 'om')."\n\n"; //adds backslashes in front all lowercase alphabet echo addcslashes($str, 'a..z')."\n"; //adds backslashes in front all uppercase alphabet echo addcslashes($str, 'A..Z')."\n"; ?>
The output of the above code will be:
J\ohn l\oves Pr\ogramming J\ohn l\oves Pr\ogra\m\ming J\o\h\n \l\o\v\e\s P\r\o\g\r\a\m\m\i\n\g \John loves \Programming
❮ PHP String Reference