string_with.php = $ndlen substr_compare($haystack, $needle, -$ndlen) === 0);}/** * 将内容字符串中的变量替换掉. * * @param string $content 内容字符串 * @param array $context 变量数组 * @param string $prefix 变量前置符
= $ndlen &&
substr_compare($haystack, $needle, -$ndlen) === 0);
}
/**
* 将内容字符串中的变量替换掉.
*
* @param string $content 内容字符串
* @param array $context 变量数组
* @param string $prefix 变量前置符号
* @param string $subfix 变量后置符号
* @return string 当前内容
*/
function replace_with($content, array $context = [], $prefix = '', $subfix = '')
{
if (empty($context)) {
return $content;
}
if (empty($prefix) && empty($subfix)) {
$replacers = $context;
} else {
$replacers = [];
foreach ($context as $key => $value) {
$replacers[$prefix . $key . $subfix] = $value;
}
}
$content = strtr($content, $replacers);
return $content;
}
