この機能を、 "マップ"(および置換)、私のWebアプリケーションシステムの一部である:
function replace($search, $replace, $mixed)
{
if (is_string($mixed)) {
return @str_replace($search, $replace, $mixed);
} else if (is_array($mixed)) {
foreach ($mixed as $k => $v) {
$mixed[$k] = replace($search, $replace, $v);
}
return $mixed;
} else {
return $mixed;
}
}
function map($a, $contents, $case_sensitive=false)
{
if (!is_array($a)) {
return $contents;
}
if (!$case_sensitive) {
$a = array_change_key_case($a);
}
$s = array();
$r = array();
foreach ($a as $k => $v) {
if (is_scalar($v) || empty($v)) {
$s[] = "{".$k."}";
$r[] = $v;
}
}
if (!$case_sensitive) {
$contents = preg_replace_mixed('/{([-_ =,.\/\'"A-Za-z0-9]+)}/ei', "'{'.strtolower('\\1').'}'", $contents);
}
return replace($s, $r, $contents);
}
非常に良い仕事をしています。括弧で囲まれた変数名で任意の文字列を指定すると、そのトリックが実行されます。
構文は異なりますが、あなたの目的のために変更することができます
$str = map(array('name' => 'Joe'), 'My name is {name}');
私は%構文上で、それを好みます。
なぜWikiですか? –
おそらく著者の側のエラー –