Current Location: Home> Function Categories> addcslashes

addcslashes

Escape characters in a string using backslashes in C style
Name:addcslashes
Category:String
Programming Language:php
One-line Description:Returns a string that adds a backslash before the specified character.

Definition and usage

addcslashes() function returns a string that adds a backslash before the specified character.

Note: addcslashes() function is case sensitive.

Note: Be careful when applying addcslashes() to the following characters: 0 (NULL), r (carriage return), n (line break), f page break), t (tab), and v (vertical tab). In PHP, \0, \r, \n, \t, \f and \v are predefined escape sequences.

Example

Example 1

Add a backslash before the character "A":

 <?php
$str = addcslashes ( "A001 A002 A003" , "A" ) ;
echo ( $str ) ;
?>

Try it yourself

Example 2

Add a backslash to a specific character in a string:

 <?php
$str = "Welcome to Shanghai!" ;
echo $str . "<br>" ;
echo addcslashes ( $str , 'm' ) . "<br>" ;
echo addcslashes ( $str , 'H' ) . "<br>" ;
?>

Try it yourself

Example 3

Add a backslash to characters within a range in a string:

 <?php
$str = "Welcome to Shanghai!" ;
echo $str . "<br>" ;
echo addcslashes ( $str , 'A..Z' ) . "<br>" ;
echo addcslashes ( $str , 'a..z' ) . "<br>" ;
echo addcslashes ( $str , 'a..g' ) ;
?>

Try it yourself

grammar

 addcslashes ( string , characters )
parameter describe
string Required. Specifies the string to be escaped.
Characters Required. Specifies the characters or range of characters to be escaped.
Similar Functions
Popular Articles