不确定PHP是否可以这样做,所以我想我会问专家:
我想要一个字符串数组,其中我有一个占位符:
array (
1 => "this is string $1",
2 => "this is string $2");
然后我想在很多页面中引用它并传递依赖于页面的值:
e.g。 print(array ['1'],“替换$ 1的值”)
这可能吗?
由于
答案 0 :(得分:3)
我建议使用sprintf()
echo sprintf('this string is %s', $array[1]);
答案 1 :(得分:1)
我建议用正则表达式替换带有文本的标记。 http://www.php.net/manual/en/function.preg-replace.php
示例:
$template = "Invoice was issued on <<ISSUED-DATE>> and has to be paid on <<DUE-DATE>> at the latest";
$patterns = array(
"/".preg_quote("<<ISSUED-DATE>>")."/",
"/".preg_quote("<<DUE-DATE>>")."/"
);
$replacements = array(
$issued,
$due
);
$newtext = preg_replace($patterns, $replacements, $template);