php模板引擎,preg_replace
嘿,我正在尝试制作一个模板引擎,但是遇到了if语句的问题php模板引擎,preg_replace,php,preg-replace,pcre,Php,Preg Replace,Pcre,嘿,我正在尝试制作一个模板引擎,但是遇到了if语句的问题 $this->template = preg_replace('~@if\((.*?)\)~', '?php if($1): ?', $this->template); $this->template = preg_replace('~@elseif\((.*?)\)~', '<?php elseif($1): ?>', $this->template); $this->templ
$this->template = preg_replace('~@if\((.*?)\)~', '?php if($1): ?', $this->template);
$this->template = preg_replace('~@elseif\((.*?)\)~', '<?php elseif($1): ?>', $this->template);
$this->template = preg_replace('~@else~', '<?php else: ?>', $this->template);
$this->template = preg_replace('~@endif~', '?php endif; ?', $this->template);
变成
<?php if(!isset($active): ?>) Show @endif
要描述括号之间包含的内容,最终嵌套括号,模式为(例如“if”模式):
(?-1)
表示上次打开的捕获组中的子模式。因为它在捕获组本身中,所以可以获得一个递归。(请注意,您也可以使用捕获组的编号调用子模式,使用(?1)
,而不是像示例中那样以相对方式调用,但是当一个模式包含多个捕获组时,相对方式更有用。)
当字符串格式不正确时,模式使用所有格量词*+
(禁止回溯)失败得更快。嵌套方式的匹配对(此处:(
和)
)不能用正则表达式所属的正则语言表示。您至少需要一种上下文无关的语言。但是,.Oh,非常感谢,现在也更有意义了:)下一个问题是:我怎么能忽略字符串中的括号?@Gumbo:要做到这一点,你必须用[^()]*+[^()”]*+(?:(?:“[^”\]*+(?:“[^”\\]*+”+“[^”\]*+(?s:\\.\.\\\\\]*+”)[^\\]*+(?s:\.\.\.\\\\\\\]*+)[^()”)[^()”)*+>
<?php if(!isset($active): ?>) Show @endif
~@if\(([^()]*+(?:\((?-1)\)[^()]*)*+)\)~