Reputation: 9289
I would like to replace each \x
to $matches[x]
where x is a number.
It works only for predefined numbers with str_replace
:
str_replace( array(
'\\1',
'\\2',
'\\3',
'\\4'
), array(
'$matches[1]',
'$matches[2]',
'$matches[3]',
'$matches[4]'
), $string );
Upvotes: 1
Views: 41
Reputation: 883
Use regular expression in preg_replace
Code:
<?php
$str = '\\2 string \\123 gogog \\123 sda \\342 \\3525 wqe \\234';
echo preg_replace('~(\\\\)(\d+)~', '$matches[$2]', $str);
Output:
$matches[2] string $matches[123] gogog $matches[123] sda $matches[342] $matches[3525] wqe $matches[234]
Upvotes: 1