回答
考慮這樣一個例子使用:
$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的preg_replace
- 4. PHP的preg_replace
- 5. PHP的preg_replace
- 6. PHP的preg_replace [於]
- 7. PHP - 中的preg_replace
- 8. PHP UTF8的preg_replace
- 9. PHP preg_replace
- 10. PHP preg_replace
- 11. preg_replace PHP
- 12. php preg_replace
- 13. PHP preg_replace \
- 14. preg_replace「.php」?
- 15. 使用PHP的preg_replace
- 16. PHP的preg_replace()函數
- 17. PHP的preg_replace幫助
- 18. PHP的preg_replace模式
- 19. PHP的問題preg_replace()
- 20. PHP的preg_replace問題
- 21. 簡單的PHP preg_replace
- 22. PHP的preg_replace問題
- 23. 使用的preg_replace PHP
- 24. PHP的preg_replace錯誤
- 25. 的preg_replace多行(PHP)
- 26. PHP動態的preg_replace
- 27. 使用PHP的preg_replace
- 28. PHP的preg_replace函數
- 29. 的preg_replace(PHP函數)
- 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