addcslashes() function in PHP 8.5 with Example

The addcslashes() function in PHP 8.5 works the same as in previous versions. It returns a string with backslashes added before the characters you specify.

Basic Syntax

addcslashes(string $string, string $characters): string

Parameters:

$string: The input string to be escaped.
$characters: A list of characters to escape. You can also specify a range like "A..z".

Example 1: Escape Specific Characters

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

Output:

Hello World!
This adds a backslash before the character "W".

Example 2: Escape Multiple Characters

<?php
echo addcslashes("where is the party?", "party");
?>

Output:

whe e is he pa y?
This escapes every occurrence of the characters p, a, r, t, and y.