PHP addcslashes() Function

PHP addslashes() Function
PHP chunk_split() Function

In this article, you will learn;

  • How to add backslashes in front of the specified characters in the string.

The PHP addcslashes() function returns a string by adding backslashes at the beginning of the specified character in the string.

PHP pre-defined escape sequence of characters is;

  • \0 – NULL
  • r – Carriae return
  • n – newline
  • f – form feed
  • t – tab
  • v – verticle tab

Note: You must consider and take care of the above-mentioned predefined escape characters in PHP while using the addcslahes() function.

Note: The addcslashes() method shows case insensitive behavior.

What is the syntax of the ADDCSLASHES() function in PHP?

addcslashes(string,characters)
ParameterDescription
stringThe string to be escaped – Required
charactersCharacter to be escaped. Also can be range of characters – Required
PHP ADDCSLASHES() method

Example of the ADDCSLASHES() function

Example 1. In this example, we add a backslash before the character P.

<?php
$str = addcslashes("Hello PHP!","P");
echo($str);
?>

Example 1. In this example, we add backslashes before the letters H and S.

<?php
$str = "Home Sweet Home";
echo $str."<br>";
echo addcslashes($str,'S')."<br>";
echo addcslashes($str,'H')."<br>";
?>

Example 1. In this example, we add backslashes to the all the letters within the range.

<?php
$str = "Home Sweet Home!";
echo $str."<br>";
echo addcslashes($str,'A..Z')."<br>";
echo addcslashes($str,'a..z')."<br>";
echo addcslashes($str,'a..h');
?>

PHP addslashes() Function
PHP chunk_split() Function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top