3v4l.org

run code in 500+ PHP versions simultaneously
<?php $articleText = <<<TEXT Some article text Triple id: {gameid 45735 76352 87262}. Do not honor {gameid 12345 67890} because contains exactly 2 game ids! unknown id: {gameid 66666} can't replace it! Found single id: {gameid 76352} finished the article TEXT; $gamesLookup = [ 45735 => 'Pac-man', 76352 => 'Donkey Kong', 87262 => 'Rampage' ]; echo preg_replace_callback( '~{gameid (\d{5})(?: (\d{5}) (\d{5}))?}~', function ($m) use ($gamesLookup) { echo 'm = ' . var_export($m, true) . "\n---\n"; return 'Game Name(s): ' . strtr(implode(', ', array_slice($m, 1)), $gamesLookup); }, $articleText );
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
m = array ( 0 => '{gameid 45735 76352 87262}', 1 => '45735', 2 => '76352', 3 => '87262', ) --- m = array ( 0 => '{gameid 66666}', 1 => '66666', ) --- m = array ( 0 => '{gameid 76352}', 1 => '76352', ) --- Some article text Triple id: Game Name(s): Pac-man, Donkey Kong, Rampage. Do not honor {gameid 12345 67890} because contains exactly 2 game ids! unknown id: Game Name(s): 66666 can't replace it! Found single id: Game Name(s): Donkey Kong finished the article

preferences:
35.41 ms | 1138 KiB | 7 Q