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
  • Set the first character of the string to uppercase ucfirst

    ucfirst

    Setthefirstcharacter
  • Reverse a string strrev

    strrev

    Reverseastring
  • Find the first occurrence of a string strstr

    strstr

    Findthefirstoccurren
  • Break string to a specified number of strings wordwrap

    wordwrap

    Breakstringtoaspecif
  • Write formatted strings to stream vfprintf

    vfprintf

    Writeformattedstring
  • Alias ​​of implode join

    join

    Alias​​ofimplode
  • Convert binary data to hexadecimal representation bin2hex

    bin2hex

    Convertbinarydatatoh
  • Set the first character of the string to lowercase lcfirst

    lcfirst

    Setthefirstcharacter
Popular Articles