Good day.
There is an expression

/(?P<name>[0-9]+)/ 

It creates a named link with the name name, but how to use it in the replacement string? That is, I would like something like

 preg_replace($re, "\{name}", $text); 

Option with \1 not to offer.
In Perl, this effect is achieved in this way:

 s/(?P<name>[0-9]+)/$+{name}/ 

but $+ is a construction of the language itself, not regular expressions.
How to achieve this in PHP?
Added by
At random tried options:

 \{name} \\{name} \k{name} \g{name} ${name} $name $+{name} %+{name} 

We need an analogue of $ + in PHP variations.

  • I asked the same question on Stack Overflow . Apparently Google translator didn’t translate something wrong :) - ReinRaus
  • put + on SO, brotherly, and then zaminusovali you there ... - johniek_comp
  • @johniek_comp, thank you :) I'm not so hot in English. On this topic, all Google broke, did not find anything. Release Notes to PCRE and preg_ * will probably have to be smoked :( - ReinRaus

2 answers 2

Regular PCRE expressions. Backlinks.

You can specify a reciprocal link to a named submask using (?P=name) or, starting with PHP 5.2.2, \k<name> or \k'name' . In addition, support for \k{name} and \g{name} was added in PHP 5.2.4.

PS The search for an answer should always begin with official documentation.

  • Wrong unfortunately. In the replacement expression, this does not work. - ReinRaus
  • one
    Well, in that case, the answer is no way. - Ilya Pirogov

Can. The syntax in the replacement string is $ name

 echo preg_replace('/(xyi)/', "тут было написано '$1'", "str"); 
  • Wrong. You misunderstand the question. - ReinRaus