<?php
function interpolate($str, array $context = [], $asIs = false)
{
$replace = [];
foreach ($context as $key => $val) {
if ($asIs) {
$replace[$key] = $val;
} else {
$replace['{{' . $key . '}}'] = $val;
}
}
return strtr($str, $replace);
}
echo interpolate('user-id-513', [
'-' => '/',
'--' => '-',
], true);
echo '<br/>';
echo interpolate('wizard-add--a--photo', [
'-' => '/',
'--' => '-',
], true);
preferences:
65.39 ms | 402 KiB | 5 Q