addcslashes – PHP String Functions

Syntax :

string addcslashes (string, charlist);

Description :

addcslashes function will add backslash before the specified characters in charlist parameter for the string.

Parameter :

  • string –  This is a Required parameter. which needs to be escaped.
  • charlist – This is a Required parameter. It can have one characters or collection of characters.

Output :

This will return the escaped string before the specified characters in charlist.

Related articles : addslashes(), stripcslashes(), stripslashes(), htmlspecialchars().

addcslashes – PHP Functions Example 1 :
$strExample = addcslashes("Hi from ","e");

In above example ,We have string “Hi from ” in which we want to escape letter “e”. So, we are applying addcslashes function.
When we apply it. This will add backslash(\) in front of every letter  “e”. Now Output will become –

“Hi from tutorialmin\es.n\et “.

addcslashes – PHP Functions Example 2 :

If you want to add backslash to mulitiple letters, Use below example :

$strExample = "Welcome to WebSite!";
echo $strExample."<br>";
echo addcslashes($strExample,'A..Z')."<br>";
echo addcslashes($strExample,'a..z')."<br>";

Output will be like below:

Welcome to WebSite!
\Welcome to \Web\Site!
W\e\l\c\o\m\e \t\o \t\u\t\o\r\i\a\l\m\i\n\e\s.\n\e\t W\e\bS\i\t\e!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *