<?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 PHP_EOL;
echo interpolate('wizard-add--a--photo', [
'-' => '/',
'--' => '-',
], true);
preferences:
35.63 ms | 402 KiB | 5 Q