Primoz Rome
Primoz Rome

Reputation: 11061

preg_replace digits and use in results

How would I use preg_replace to replace:

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

into this:

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

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

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

This gets me this result:

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

Upvotes: 3

Views: 52

Answers (1)

Kasravnd
Kasravnd

Reputation: 107357

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

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

Upvotes: 4

Related Questions