<?php $num = "011223333"; $res = preg_replace_callback( '/(\d)\1*/', fn($match) => strlen($match[0]) . $match[0][0], $num ); var_dump($res); //102243
Tag: php
Look and Say Sequence in PHP
<?php echo implode("<br>", lookAndSay(0, 5)); function lookAndSay(string $num, int $count): array { $results = [$num]; for ($i = 0; $i < $count; $i++) { preg_match_all('/(\d)\1*/', $num, $matches); $num = implode('', array_map(function ($item) { return strlen($item) . $item[0]; }, $matches[0])); $results[] = $num; } return $results; }