回答
考虑这样一个例子使用:
$rcontent = "abc {{foo}} def";
$foo = 'bar';
$rcontent = preg_replace("/{{(.*?)}}/e","$$1",$rcontent);
echo $rcontent; // prints abc bar def
我假设你正在分配的preg_match
值回到$rcontent
,否则它没有任何意义。
现在正在使用的正则表达式是{{(.*?)}}
看起来对{{
和}}
之间的任何(非greedyly),还记得因为括号匹配的字符串。
在我的情况下,.*?
匹配foo
。
接下来的替换部分是$$1
。现在$1
是foo
,所以$$1
将是$foo
这就是bar
。因此{{foo}}
将被替换为$foo
的值bar
。
如果$$1
仅仅是一个类型,你想用$1
那么正则表达式替换{{foo}}
与foo
。
$ file = file(“templates/index.html”); $ rcontent = join(“”,$ file); $ rcontent = preg_replace(“/{{(.*?)}}/ e”,“$$ 1”,$ rcontent); print $ rcontent; 这是我的code.its工作完美..但模板/ index.html文件没有任何palenthesis它.. – sowmiya 2010-10-20 06:13:28
@sowmiya:对不起,我没有得到你的问题。 – codaddict 2010-10-20 07:31:36
正在将“templates/index.html”的内容复制到一个php文件中....要使用此正则表达式复制内容,我在“templates/index.html”文件中没有任何大括号。但它完美的作品..如何这个模板引擎工作我不知道... – sowmiya 2010-10-20 08:50:46
lazy * 重复上一项零次或多次。懒惰,所以引擎首先尝试跳过前一个项目,然后尝试排列前一个项目不断增加的匹配项。
为如:.*?
在abc "def" "ghi" jkl
- 1. 的PHP的preg_replace
- 2. PHP的preg_replace
- 3. PHP UTF8的preg_replace
- 4. PHP的preg_replace
- 5. PHP的preg_replace [于]
- 6. preg_replace PHP
- 7. PHP preg_replace
- 8. PHP preg_replace \
- 9. preg_replace“.php”?
- 10. 使用PHP的preg_replace
- 11. PHP的preg_replace问题
- 12. 使用PHP的preg_replace
- 13. PHP的preg_replace问题
- 14. PHP的preg_replace网址
- 15. 的preg_replace(PHP函数)
- 16. PHP的问题preg_replace()
- 17. PHP的preg_replace问题
- 18. PHP的preg_replace帮助
- 19. 使用的preg_replace PHP
- 20. 简单的PHP preg_replace
- 21. PHP的preg_replace函数
- 22. PHP的preg_replace()函数
- 23. PHP的preg_replace问题
- 24. PHP的preg_replace错误
- 25. PHP的preg_replace模式
- 26. preg_replace with php code
- 27. PHP preg_replace多次
- 28. php preg_replace挫折
- 29. PHP preg_replace BESbswy ckeditor
- 30. PHP preg_replace问题
匹配
"def"
为什么投票关闭这个?很明显,问题在于询问正则表达式的含义。 – JAL 2010-10-20 05:00:06@亚历克斯JL:我没有投票结束,但它一定是标题。我不知道元标签与这个问题有什么关系。 – BoltClock 2010-10-20 05:24:04
@BoltClock它很神秘。 – JAL 2010-10-20 06:30:43