PHP addcslashes() Function
In this chapter you will learn:
- Definition for PHP addcslashes() Function
- Syntax for PHP addcslashes() Function
- Parameter for PHP addcslashes() Function
- Return for PHP addcslashes() Function
- Example - Add a backslash in front of the character "W"
- Example - Add backslashes to certain characters in a string
- Example - Add backslashes to a range of characters in a string
Definition
The addcslashes() function returns a string with backslashes added in front of the specified characters.
The addcslashes() function is case-sensitive.
Syntax
PHP addcslashes() function has the following syntax.
addcslashes(string,characters)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to be escaped |
characters | Required. | Characters or range of characters to be escaped |
Return
PHP addcslashes() function returns the escaped string.
Example 1
Add a backslash in front of the character "W"
<?php
$str = addcslashes("Hello World!","W");
echo($str);
?>
The code above generates the following result.
Example 2
Add backslashes to certain characters in a string:
<?php/*j a va 2s. c o m*/
$str = "Welcome to java2s.com!";
echo $str."\n";
echo addcslashes($str,'m')."\n";
echo addcslashes($str,'H')."\n";
?>
The code above generates the following result.
Example 3
Add backslashes to a range of characters in a string:
<?php/*from j a v a2s.co m*/
$str = "Welcome to java2s.com!";
echo $str."\n";
echo addcslashes($str,'A..o')."\n";
echo addcslashes($str,'a..o')."\n";
echo addcslashes($str,'a..j');
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP addslashes() Function
- Syntax for PHP addslashes() Function
- Parameter for PHP addslashes() Function
- Return for PHP addslashes() Function
- Note for PHP addslashes() Function
- Example - Escape quotation
Home » PHP Tutorial » PHP String Functions