搜索 [ ] 并替换其之间的所有内容
我有一个使用该结构的模板文件 [名] [LASTNAME]
等等等等......我将对其进行搜索和替换。我想做的一件事是,当模板被发回时,如果我没有规定,[FIRSTNAME]....它仍然显示在模板中...如果我没有规定,我想将其设置为 NULL没有为该变量规定任何数据。
在我的代码中,我正在使用 FILE_GET_CONTENTS
$q = file_get_contents($filename);
foreach ($this->dataArray as $key => $value)
{
$q = str_replace('['.$key.']', $value, $q);
}
return $q;
因此,一旦该循环结束填写正确的数据,我需要进行另一次搜索并替换剩下使用的任何变量,[FIRSTNAME]
我希望这有意义,任何人都可以引导我正确的方向。
干杯
I have a template file that uses the structure
[FIRSTNAME]
[LASTNAME]
etc etc.... and I will be doing a search and replace on it. One thing that I would like to do is that when the template get's sent back, IF I haven't stipulated, [FIRSTNAME].... it still shows in the template... I would like to make it NULL IF I haven't stipulated any data to that variable.
in my code i'm using the FILE_GET_CONTENTS
$q = file_get_contents($filename);
foreach ($this->dataArray as $key => $value)
{
$q = str_replace('['.$key.']', $value, $q);
}
return $q;
So once that loop is over filling in the proper data, I need to do another search and replace for any variables left using, [FIRSTNAME]
I hope this makes sense any someone can steer me in the right direction.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您只想添加一行,例如:
在所有定义的替换之后,消除任何剩余的方括号单词。
如果你想简洁,你可以用“一行模板引擎”的变体来替换整个函数 http://www.webmasterworld.com/php/3444822.htm,用方括号代替花括号。
It sounds like you just want to add a line like:
After all your defined substitutions, to eliminate any remaining square-bracketed words.
If you want to be concise, you can replace the whole function with a variation of the "one line template engine" at http://www.webmasterworld.com/php/3444822.htm, with square brackets instead of curlies.
实际上,您可以将数组作为参数传递到 str_replace 函数中。
如果您想删除名字(如果名字为空),为什么不将其添加到键 =>替换数组的值为
''
?或者类似的东西
You can actually pass arrays into the str_replace function as arguments.
And if you want to remove first name if its blank, why don't you just add it to the key => replacement array with a value of
''
?or something like