基本上,我想实现的是动态替换{SOME_TAG}
与“文本”的内容。
我的想法是读取所有标签,如{SOME_TAG}
,把它们放到数组。
然后转换数组键为变量,如$some_tag
,并把它们放到数组。PHP替换字符串标签/占位符/标记与动态值
所以,这是我在多大程度上得到:
//Some code goes here
$some_tag = "Is defined somewhere else.";
$different_tag = 1 + $something;
Some text {SOME_TAG} appears in different file, which contents has been read earlier.
//Some code goes here
preg_match_all('/{\w+}/', $strings, $search);
$search = str_replace(str_split('{}'),"",$search[0]);
$search = array_change_key_case(array_flip($search), CASE_LOWER);
...some code missing here, which I cant figure out.
更换阵列应该是这个样子
$replace = array($some_tag, $different_tag);
//Then comes replacing code and output blah blah blah..
如何使阵列$replace
包含变量动态地根据$search
阵列?
这没有意义。 – silkfire 2013-03-15 13:08:21
您希望我们如何帮助..请正确编辑您的问题,以便其他人能够理解。另外请不要尝试复制粘贴代码。你不应该说“这里有些代码缺失,我无法弄清楚”?这是什么意思 ? – 2013-03-15 13:13:05