Pavel Koryagin Pavel Koryagin - 8 months ago 48
PHP Question

preg_replace produces unexpected duplicate with a very simple RegEx

preg_replace('/.?$/s', "x", 'abc'),
preg_replace('/.$/s', "x", 'abc')


string(4) "abxx" <-- strange, isn't it?
string(3) "abx"

How's that possible?


preg_replace replaces all matches in the string. So after doing each replacement, it searches for another match starting after the place where it made the previous replacement.

So it first looks for /.?$/ and finds c, and replaces it with x, resulting in abx. Then, it looks again for /.?$/ starting right after the x. Since .? means to match 0 or 1 characters, it matches the zero-length string at the end of the string, and replaces it with x. Then it stops because it has reached the end of the string.