3v4l.org

run code in 300+ PHP versions simultaneously
<?php function template($template, $vals) { $matches = array(); $regex = "/\{\{(\S+)\}\}/e"; $rendered = $template; // in case no matches are found preg_match_all($regex, $template, $matches, PREG_OFFSET_CAPTURE, 3); foreach ($matches[0] as $found) { $block = $found[0]; $offset = $found[0]; $k = str_replace('{{', '', $block); $k = str_replace('}-}', '', $k); $rendered = preg_replace($regex, '$vals[\\1]', $template); } return $rendered; } function template2($template, $vals) { $matches = array(); $regex = "/\{\{(\S+)\}\}/e"; $rendered = $template; // in case no matches are found preg_match_all($regex, $template, $matches, PREG_OFFSET_CAPTURE, 3); foreach ($matches[0] as $found) { $block = $found[0]; $offset = $found[0]; $k = str_replace('{{', '', $block); $k = str_replace('}-}', '', $k); $rendered = preg_replace_callback($regex, function($matches) { return $matches[0]; }, $template); } return $rendered; } $template = "test {{ME}}."; $vals = array('ME' => 'beep'); echo template($template, $vals);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: preg_match_all(): The /e modifier is no longer supported, use preg_replace_callback instead in /in/CgLXW on line 9 Warning: Undefined array key 0 in /in/CgLXW on line 11 Warning: foreach() argument must be of type array|object, null given in /in/CgLXW on line 11 test {{ME}}.
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(): The /e modifier is no longer supported, use preg_replace_callback instead in /in/CgLXW on line 9 Warning: Undefined array key 0 in /in/CgLXW on line 11 Warning: foreach() argument must be of type array|object, null given in /in/CgLXW on line 11 test {{ME}}.
Output for 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Warning: preg_match_all(): The /e modifier is no longer supported, use preg_replace_callback instead in /in/CgLXW on line 9 Notice: Undefined offset: 0 in /in/CgLXW on line 11 Warning: Invalid argument supplied for foreach() in /in/CgLXW on line 11 test {{ME}}.
Output for 7.3.32 - 7.3.33
Warning: preg_match_all(): The /e modifier is no longer supported, use preg_replace_callback instead in /in/CgLXW on line 9 Warning: Invalid argument supplied for foreach() in /in/CgLXW on line 11 test {{ME}}.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33
Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /in/CgLXW on line 18
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /in/CgLXW on line 18 Notice: Use of undefined constant ME - assumed 'ME' in /in/CgLXW(18) : regexp code on line 1 test beep.

preferences:
199.51 ms | 402 KiB | 198 Q