3v4l.org

run code in 300+ PHP versions simultaneously
<?php function updateYear(string $text, int $newYear): string { $year = null; $result = preg_replace_callback( '/(\-)(\d\d\d\d)/', function (array $matches) use ($newYear, &$year): string { $year = (int) $matches[2]; return $matches[1] . (string) $newYear; }, $text ); return "Replaced {$year} in '{$text} => '{$result}'"; } echo updateYear('(c) 1970-2022', 2023) . PHP_EOL; echo updateYear('(c) 1999-2022', 2023) . PHP_EOL;
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Replaced 2022 in '(c) 1970-2022 => '(c) 1970-2023' Replaced 2022 in '(c) 1999-2022 => '(c) 1999-2023'

preferences:
112.06 ms | 402 KiB | 85 Q