2012-11-09 66 views
0

我是一個加標籤報告系統上工作,其中一個文本塊將存儲這樣的信息:{if}個評估在一個字符串

This is a line with a {TAG} . The {TAG} will be replaced with normal text via str_replace. 
{if 1==1} 
    I would like to be able to do if statements like this. 
{/if} 

所以{TAG}被替換很容易,但有辦法解析一個文本塊來處理if/else語句?

+0

是的,可能的。各種模板引擎實現(通常通過將這些佔位符轉換爲一個PHP腳本)。使用其中之一,因爲它是複雜的代碼在你自己的。 – mario

回答

1

在純粹的PHP中,如果你使用heredoc語法,你將無法做到如果塊內的字符串。

如果您能夠接受連接字符串,您可以使用三元運算符是這樣的:

$myString = 
    "This is a line with a {TAG}. " . 
    "The {TAG} will be replaced with normal text via str_replace. " . 
    (1==1 ? "I would like to be able to do if statements like this." : "") . " " 
; 

希望有所幫助。