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 tutorialmines.net ","e");
echo($strExample);

In above example ,We have string “Hi from tutorialmines.net ” 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 tutorialmines.net WebSite!";
echo $strExample."<br>";
echo addcslashes($strExample,'A..Z')."<br>";
echo addcslashes($strExample,'a..z')."<br>";

Output will be like below:

Welcome to tutorialmines.net WebSite!
\Welcome to tutorialmines.net \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 *