PHP Tutorial - 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
$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
$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.