Friday, September 5, 2025
HomeLanguagesPHP addcslashes() Function

PHP addcslashes() Function

The addcslashes() function is a built-in function in PHP. The addcslashes() function is used to add backslashes before some specified characters in a given string.

Syntax:

string addcslashes($string, $characters)

Parameters: This function accepts two parameters as shown in the above syntax and are described below:

  1. $string: This parameter specifies the input string which is needed to be escaped. Or we can also say that the string in which we want to add backslashes before some specified characters.
  2. $characters: This parameter specifies a character or sequence of characters which we want to escape in the input string by adding backslashes before them. We can specify a range of characters as ‘a..z’. That is the start character of the range followed by two dots and the ending character.
    Note: Please use characters like a,b,n,t etc carefully as this parameter as \a,\b,\n,\t are predefined escape sequences and have some special meaning. So, we might not get the desired result.

Return Value: This function returns a escaped string which is the input string $string with backslashes added before $characters.

Examples:

Input: $string = "neveropen"  $characters = 'e'
Output: G\e\eksforG\e\eks

Input: $string = "neveropen" $characters = 'a..k'
Output: G\e\e\ksnG\e\e\ks

Below programs illustrate the addcslashes() function in PHP:

Program 1:




<?php
// PHP program to illustrate addcslashes()
// function
  
$str = "neveropen";
  
$resStr = addcslashes($str, 'e');
  
echo $resStr;
  
?>


Output:

G\e\eksforG\e\eks

Program 2:




<?php
// PHP program to illustrate addcslashes()
// function
  
$str = "GeeksnGeeks";
$resStr = addcslashes($str, 'a..k');
  
echo $resStr;
  
?>


Output:

G\e\e\ksnG\e\e\ks

Reference:
http://php.net/manual/en/function.addcslashes.php

RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS