<?php $num = "011223333"; $res = preg_replace_callback( '/(\d)\1*/', fn($match) => strlen($match[0]) . $match[0][0], $num ); var_dump($res); //102243
<?php $num = "011223333"; $res = preg_replace_callback( '/(\d)\1*/', fn($match) => strlen($match[0]) . $match[0][0], $num ); var_dump($res); //102243