Preg_replace numbers and use in results

How to use preg_replace to replace:

Some text is [[1]] and some is [[2]] bla bla...

      

in it:

Some text is 1.____________ and some is 2.____________ bla bla...

      

I have this regex that finds the correct occurrences for me, but I don't know how to add the replacement digits to the result?

preg_replace('(\[\[\d\]\])', '_______', $subject);

      

It gives me this result:

Some text is ____________ and some is ____________ bla bla...

      

+3


source to share


1 answer


You need to use a capture group and a link to it:



preg_replace('~\[\[(\d)\]\]~', '$1._______', $subject);

      

+4


source







All Articles