- preg_replace_callback: documentation ( source)
- sprintf: documentation ( source)
<?php
$string = "This is my test string {ABC}. This is test {XYZ}. I am new for PHP {PHP}".
$i = 1;
echo preg_replace_callback('/\{(.+?)\}/', function (array $match) use (&$i) {
return sprintf('{%s %d}', $match[1], $i++);
}, $string);