3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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; } $template = "test {{ME}}."; $vals = array('ME' => 'beep'); echo template($template, $vals);
based on tDYS7
Output for 7.0.0 - 7.2.0
Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /in/om6KN on line 18
Output for 5.5.24 - 5.6.28
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /in/om6KN on line 18 Notice: Use of undefined constant ME - assumed 'ME' in /in/om6KN(18) : regexp code on line 1 test beep.
Output for 5.3.20 - 5.4.45
Notice: Use of undefined constant ME - assumed 'ME' in /in/om6KN(18) : regexp code on line 1 test beep.