How to replace string using overlapping regular expressions?
Hello! Im trying to add html tags into string. Im using function preg_replace(), but when my patterns in array are overlaped the preg_replace doesn't work.
$string = 'abc';
$patterns[0] = '/a/';
$patterns[1] = '/abc/';
$replacements[0] = '<b>$0</b>';
$replacements[1] = '<i>$0</i>';
echo preg_replace($patterns, $replacements, $string);
This produce the following output:
<b>a</b>bc
But i expected this output:
<b><i>a</b>bc</i>
It seems like preg_replace takes first pattern and replace it with first replacement and then search for second pattern but it search in modified string. I must use functions which support regular expressions.
<b><i>a</b>bc</i>is invalid markup? You'd need<i><b>a</b>bc</i>to be valid!