/** * Interpolates {{var.subVar}}-style based on a source array given * * Dimensions in the source array are accessed with a passed delimeter (Default: Dot (.)) * * @param string $string The input string to operate on * @param array $source The associative source array * @param mixed $defaultValue The default value for indices that dont exist * @param string|null $delimeter The delimeter for multi-dimension access (Default: Dot (.)) * * @return string The interpolated string with the variables replaced with their values */ public static function interpolate($string, array $source, $defaultValue = null, $delimeter = null) { return preg_replace_callback('/\\{\\{([^\\}]+)\\}\\}/i', function ($m) use($source, $defaultValue, $delimeter) { $resolved = ArrayUtil::resolve($source, $m[1], $defaultValue, $delimeter); if ($resolved === null) { return $m[0]; } return $resolved; }, $string); }