The `addcslashes()` function in PHP 7.4 is used to escape specific characters within a string by adding a backslash (`\`) in front of them. You can specify the characters to be escaped using a character list or a range.
Here’s the syntax:
addcslashes(string $str, string $charlist): string
– `$str`: The string in which to escape characters.
– `$charlist`: The list of characters to escape.
Example 1: Escaping specific characters
<?php $str = "Hello World!"; $escaped_str = addcslashes($str, "W!"); echo $escaped_str; ?>
Output:
Hello \World\!
In this example, the characters `W` and `!` are escaped with backslashes.
Example 2: Escaping a range of characters
<?php $str = "abcxyz"; $escaped_str = addcslashes($str, 'a..c'); echo $escaped_str; ?>
Output:
\a\b\cxyz
In this example, the range `a..c` is used to escape the characters `a`, `b`, and `c`.