3v4l.org

run code in 300+ PHP versions simultaneously
<?php $items = json_decode('[ {"title":"Title #1", "text":"Text #1"}, {"title":"Title #2", "text":"Text #2"} ]'); $itemTmpl = "<h3 class='foo'>{title}</h3><div class='bar'>{text}</div>"; $html = []; foreach ($items as $item) { $html[] = preg_replace_callback( '/{([a-z]+)}/', fn($m) => $item->{$m[1]}, $itemTmpl ); } var_export($html);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
array ( 0 => '<h3 class=\'foo\'>Title #1</h3><div class=\'bar\'>Text #1</div>', 1 => '<h3 class=\'foo\'>Title #2</h3><div class=\'bar\'>Text #2</div>', )

preferences:
91.22 ms | 407 KiB | 5 Q