<?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.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- 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:
128.13 ms | 402 KiB | 121 Q