2012-11-09 65 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." : "") . " " 
; 

希望有所帮助。