⊗ppPmRgLE 243 of 447 menu

Escaping Delimiters in PHP

If a character is not special, then, when you use it as a delimiter, it will need to be escaped within the regex itself. Let's look at an example.

Let's say we are using hash signs as delimiters, and inside the regex we are looking for the ampersand character. Since the ampersand is not a special character, we do not escape it:

<?php echo preg_replace('#a&b#', '!', 'a&b'); // outputs '!' ?>

Now let the delimiters be ampersands and inside the regex we also need an ampersand. In this case, the ampersand inside has to be escaped, otherwise it will cause a PHP error:

<?php echo preg_replace('&a\&b&', '!', 'a&b'); // outputs '!' ?>

Fix the error made in the following code:

<?php echo preg_replace('#a#b#', '!', 'a#b'); ?>
byenru