- json_decode: documentation ( source)
- preg_replace_callback: documentation ( source)
- var_export: documentation ( source)
<?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);