【问题标题】:Regex, delete tags not content正则表达式,删除标签而不是内容
【发布时间】:2016-05-21 08:10:44
【问题描述】:

早安

我正在开发一个模板类。 我将在 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);
                }
            }
        }
}
}

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

谢谢

【问题讨论】:

  • 也许strip_tags 可以提供帮助
  • 我不明白我该怎么做。我在 tpl 中有多个条件,我只想从条件中删除为真的标签。使用 strip_tags 我只能删除所有标签。还是我错了?

标签: php regex template-engine


【解决方案1】:

真的不安全,但我在这里使用eval(),只是为了简单起见。您可以用您的 switch() case 逻辑替换它。我认为使用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&lt;condition&gt;.*)\)\n*\s*(?P&lt;content&gt;(?s:[\=\"\s\/\&lt;\&gt;\w\n]*))@endif/m' 正则表达式here

【讨论】:

  • 感谢您的回答。但是当我写在Tpl &lt;div class="test"&gt;test&lt;/div&gt;它dosnt函数。
  • @Neonlight 通过将\=\" 添加到正则表达式来解决。我更新了答案。
  • 非常感谢。你知道学习正则表达式模式的好网站吗?
  • @Neonlight 你可以查看"8 Regular Expressions You Should Know"
猜你喜欢
  • 2015-10-05
  • 1970-01-01
  • 2014-12-20
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
相关资源
最近更新 更多