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.28, 8.1.0 - 8.1.17, 8.2.0 - 8.2.4
Replaced 2022 in '(c) 1970-2022 => '(c) 1970-2023' Replaced 2022 in '(c) 1999-2022 => '(c) 1999-2023'

preferences:
38.37 ms | 405 KiB | 5 Q