addcslashes() adds backslashes before characters selected by a character list. Be careful with ranges in the character list because they may include more characters than expected.
$text = 'A-Z 123';
echo addcslashes($text, 'A..Z');
$string="Hello welcome to plus2net Php section";
echo addcslashes($string,'e');
Output of this is here
H\ello w\elcom\e to plus2n\et Php s\ection
Note in all places char e is escaped by adding a slashes.addslashes(), addcslashes() escapes only the characters selected by the provided character list. Use a range such as a..z carefully.
Character list examples:
'aeiou' = selected vowels
'a..z' = lowercase letter range
Here is the syntax
string addcslashes(string,string or charlist)
Returns the escaped string.$str = "Welcome 'to plus2net PHP section";
echo addcslashes($str,"'");
Welcome \'to plus2net PHP section
$string="Hello welcome to plus2net Php section";
echo addcslashes($string,'con');
Output is here
Hell\o wel\c\ome t\o plus2\net Php se\cti\o\n
$string="Hello welcome to plus2net Php section";
echo addcslashes($string,'a..z');
Output is here
H\e\l\l\o \w\e\l\c\o\m\e \t\o \p\l\u\s2\n\e\t P\h\p \s\e\c\t\i\o\n
Note that all upper case chars are not escaped
$str = "Product123";
$escaped_str = addcslashes($str, '0..9');
echo $escaped_str; // Outputs: Product\1\2\3
$str = "Hello! How are you?";
$escaped_str = addcslashes($str, '!');
echo $escaped_str; // Outputs: Hello\! How are you?
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.