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.4.1 - 8.4.12
Warning: preg_match_all(): Delimiter must not be alphanumeric, backslash, or NUL byte 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.2.0 - 8.2.29, 8.3.0 - 8.3.25
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.33
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:
132.08 ms | 411 KiB | 5 Q