PHP addcslashes() Function

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

ParameterIs RequiredDescription
stringRequired.String to be escaped
charactersRequired.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/*from  w  w w.ja  v  a  2s. co  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   ww  w  .  ja va  2 s.c om*/
$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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions