Search code examples
phpregexpcre

How does preg_replace() understand '${1}1,$3' in $replacement argument?


I am having a hard time to understand the preg_replace().
This is what I have got so far. preg_replace($patter, $replacement, $string)
But, It confuses me when there is capturing groups in replacement. But still I get some of it. Like below

preg_replace('/(\w+)/', 'hello', 'say hello!')
I know it will result in 'hello hello!'. I can do something more with capturing group. preg_replace('/(\w+)/','\1 Hello', 'Say World!',1)

It will result in 'Say Hello World'.

This is what I don't get.

<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?>
Output:
April1,2003

What ${1}1,$3 means?

What's the difference between using $1 \1 and <\1> What does this ${1}1 mean?


Solution

  • Great question!

    For substitution parts

    My guess is that <\1> and $1 ${1} are pretty much the same, addressing capturing groups, except one of which has an extra <>.

    I think we are using ${1}1 since if we don't, we would have had $11, which does not simply exist, since we don't have that 11 capturing groups, we only have 3 of those, similar story for \11.

    ${1}1,$3 means the first capturing group, ${1}, and the number 1, and third capturing group, $3.

    Inside the Regular Expression

    When we use \1 or \2 inside a regular expression, not as a substitute, it would reference back the prior capturing groups $1 and $2, which is the same as \1 and 2 in the substitute section, which I also think that might create reasonable and valid confusions.

    Please see this demo for back-referencing.

    Reference

    Named Capturing Groups and Backreferences