3v4l.org

run code in 300+ PHP versions simultaneously
<?php $search = 'The'; $replace = 'A'; $subject = "The Quick Brown Fox Jumps Over The Lazy Dog's Thermos!"; function str_rreplace($search, $replace, $subject) { return (false !== $pos = strrpos($subject, $search)) ? substr_replace($subject, $replace, $pos, strlen($search)) : $subject; } function preg_rreplace($search, $replace, $subject) { preg_match_all($search, $subject, $matches, PREG_SET_ORDER); return ($lastMatch = end($matches)) ? str_rreplace($lastMatch[0], $replace, $subject) : $subject; } echo str_rreplace($search, $replace, $subject) . "\n---\n" . preg_rreplace($search, $replace, $subject);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL in /in/lNU8R on line 12 Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, null given in /in/lNU8R:13 Stack trace: #0 /in/lNU8R(13): end(NULL) #1 /in/lNU8R(18): preg_rreplace('The', 'A', 'The Quick Brown...') #2 {main} thrown in /in/lNU8R on line 13
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL in /in/lNU8R on line 12 Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, null given in /in/lNU8R:13 Stack trace: #0 /in/lNU8R(13): end(NULL) #1 /in/lNU8R(18): preg_rreplace('The', 'A', 'The Quick Brown...') #2 {main} thrown in /in/lNU8R on line 13
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Warning: preg_match_all(): Delimiter must not be alphanumeric or backslash in /in/lNU8R on line 12 Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, null given in /in/lNU8R:13 Stack trace: #0 /in/lNU8R(13): end(NULL) #1 /in/lNU8R(18): preg_rreplace('The', 'A', 'The Quick Brown...') #2 {main} thrown in /in/lNU8R on line 13
Process exited with code 255.
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: preg_match_all(): Delimiter must not be alphanumeric or backslash in /in/lNU8R on line 12 Warning: end() expects parameter 1 to be array, null given in /in/lNU8R on line 13 The Quick Brown Fox Jumps Over The Lazy Dog's Armos! --- The Quick Brown Fox Jumps Over The Lazy Dog's Thermos!

preferences:
166.28 ms | 402 KiB | 183 Q