⊗ppPmRgSSE 236 of 447 menu

Exceptions Inside Character Classes in PHP Regex

You already know that special characters inside [] become ordinary characters. However, there are exceptions: if you need square brackets as characters, they must be escaped with a backslash.

For example, in the following code, the search pattern looks like this: between the x's is a square bracket:

<?php $str = 'x]x xax x[x x1x'; $res = preg_replace('#x[\[\]]x#', '!', $str); ?>

As a result, the following will be written to the variable:

'! xax ! x1x'

Given a string:

<?php $str = 'x[]z x{}z x.z x()z'; ?>

Write a regular expression that will find all words matching the pattern: letter 'x', then any number of any brackets, then letter 'z'.

Given a string:

<?php $str = '[abc] {abc} abc (abc) [abc]'; ?>

Write a regular expression that will find strings in any brackets and replace them with '!'.

byenru