- preg_replace_callback: documentation ( source)
<?php
$str = '# (811) (1485) [2756] {29} [555] {15}
# (811) (1476) {20} {15} (1485) [196] [2441]
# (911) (619) {19} (1476) [2765] [2752] {21}';
echo preg_replace_callback('~ {(?!(?:19|20)})(\d+)}(?! *$)| *$~m', function($m) {
static $rep = '';
$rep .= $m[0];
if ( !isset($m[1]) && [$ret, $rep] = [$rep, ''] ) return $ret;
}, $str);