0

我正在开发一个可标记的报告系统,其中一个文本块将存储如下信息:

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 语句?

4

1 回答 1

1

在纯 php 中,如果您使用 heredoc 语法,您将无法在字符串中执行 if 块。

如果你对连接字符串没问题,你可以像这样使用三元运算符:

$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." : "" ) . " "
;

希望有帮助。

于 2012-11-09T02:04:11.383 回答