正则表达式,删除不内容的标签

Regex, delete tags not content

早上好

我正在研究模板类。 我将在 tpl 文件中设置条件,如下所示:

@if('eee' == 'esee')
  <h2>Test</h2>
@endif

@if('aaa' == 'aaa')
  <h2>Test</h2>
@endif

当条件为假时,所有隐藏的工作,但如果为真,我将删除 @if('eee' == 'esee')@endif,但我不知道如何。

这里是我的函数

private static function get_condition(){
    $control = array();
    preg_match_all('/@if\((?P<control>[^)]+)\)/', self::$viewMainContent, $control);
$matches = isset($control['control']) ? count($control['control']) : 0;
for($i = 0; $i < $matches; $i++) {
        $operators = [' == ', ' != ', ' >= ', ' <= ', ' > ', ' < '];
        foreach($operators as $operator){
            if(preg_match('/'.$operator.'/', $control['control'][$i])){
                $val = explode($operator, $control['control'][$i]);
                $param = preg_grep('/\'[^"]*\'/is', $val, PREG_SET_ORDER);
                $show = false;
                if(count($param) == 2){
                    switch(trim($operator)){
                        case "==":
                            if($param[0] == $param[1]){ $show = true; }
                            break;
                        case "!=":
                            if($param[0] != $param[1]){ $show = true; }
                            break;
                        case ">=":
                            if($param[0] >= $param[1]){ $show = true; }
                            break;
                        case "<=":
                            if($param[0] <= $param[1]){ $show = true; }
                            break;
                        case ">":
                            if($param[0] > $param[1]){ $show = true; }
                            break;
                        case "<":
                            if($param[0] < $param[1]){ $show = true; }
                            break;

                    }
                }

                self::$viewMainContent = str_replace("\n", " ", self::$viewMainContent);
                if(!$show){
                    self::$viewMainContent = preg_replace('/'.preg_quote($control[0][$i]).'(.*?)\@endif/', '', self::$viewMainContent);
                } else {
                    //self::$viewMainContent = preg_replace('/'.preg_quote($control[0][$i]).'/', '', self::$viewMainContent);
                    //self::$viewMainContent = preg_replace('/@endif/', '', self::$viewMainContent);
                }
            }
        }
}
}

如何删除没有内容的标签?

感谢

真的不安全,但我用过eval() here, just for the sake of simplicity. You can replace it with your switch() case logic. I think the one of the ways to do what you want is with preg_replace_callback():

$template = <<<'EOD'
@if('eee' == 'eee')
  <h2>Test</h2>
@endif

@if('aaa' == 'aaa')
  <h2>Test</h2>
@endif
EOD;

$regex = '/@if\((?P<condition>.*)\)\n*\s*(?P<content>(?s:[\=\"\s\/\<\>\w\n]*))@endif/m';

preg_match_all($regex, $template, $matches);

$template = preg_replace_callback(
    $regex,
    function ($matches) {
        if (eval('return ' . $matches['condition'] . ';')) {
            return rtrim($matches['content']);
        }

        return '';
    },
    $template
);

var_dump($template);

这里是demo.

您还可以尝试使用 '/@if\((?P<condition>.*)\)\n*\s*(?P<content>(?s:[\=\"\s\/\<\>\w\n]*))@endif/m' 正则表达式 here