public static function insert($str, array $data, array $options = array())
{
$defaults = array('before' => '{:', 'after' => '}', 'escape' => null, 'format' => null, 'clean' => false);
$options += $defaults;
$format = $options['format'];
if ($format === 'regex' || !$format && $options['escape']) {
$format = sprintf('/(?<!%s)%s%%s%s/', preg_quote($options['escape'], '/'), str_replace('%', '%%', preg_quote($options['before'], '/')), str_replace('%', '%%', preg_quote($options['after'], '/')));
}
if (!$format && key($data) !== 0) {
$replace = array();
foreach ($data as $key => $value) {
if (!is_scalar($value)) {
if (is_object($value) && method_exists($value, '__toString')) {
$value = (string) $value;
} else {
$value = '';
}
}
$replace["{$options['before']}{$key}{$options['after']}"] = $value;
}
$str = strtr($str, $replace);
return $options['clean'] ? static::clean($str, $options) : $str;
}
if (strpos($str, '?') !== false && isset($data[0])) {
$offset = 0;
while (($pos = strpos($str, '?', $offset)) !== false) {
$val = array_shift($data);
$offset = $pos + strlen($val);
$str = substr_replace($str, $val, $pos, 1);
}
return $options['clean'] ? static::clean($str, $options) : $str;
}
foreach ($data as $key => $value) {
if (!($key = sprintf($format, preg_quote($key, '/')))) {
continue;
}
$hash = crc32($key);
$str = preg_replace($key, $hash, $str);
$str = str_replace($hash, $value, $str);
}
if (!isset($options['format']) && isset($options['before'])) {
$str = str_replace($options['escape'] . $options['before'], $options['before'], $str);
}
return $options['clean'] ? static::clean($str, $options) : $str;
}