带有自定义标签的preg_replace在php中

时间:2015-04-03 07:46:52

标签: php regex

我想解析一个自定义模板文件,但仍然在使用正则表达式。

我想解析以下文件:

@foreach(($ones) as $one)

    @foreach($twos as $two)

        multiline content

    @endforeach

@endforeach

@foreach($three as $three)

    @other_expression

    @end_other_expression

@endforeach

结果应为:

<?php foreach(($ones) as $one) { ?>

    <?php foreach ($twos as $two) { ?>

        multiline content

    <?php } ?>

<?php } ?>

<?php foreach($threes as $three) { ?>

    @other_expression

    @end_other_expression

<?php } ?>

取代@endforeach相当容易。我用这段代码做了:

$pattern = '/@endforeach/'
$replacement = '<?php } ?>';
$contents = preg_replace($pattern, $replacement, $contents);

现在我需要用以下代码替换我试过的@foreach部分:

$pattern = '/@foreach\(([^.]+)\)\\n/';
$replacement = '<?php foreach($1) { ?>';
$contents = preg_replace($pattern, $replacement, $contents);

问题是此模式无法识别我的@foreach()语句的结尾。新行的\ n不起作用。我不能使用右括号,因为foreachhead内可能有多个支架。

我愿意接受任何建议。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以连续使用2个正则表达式来执行此操作:

<?php
   $str = "@foreach((\$ones) as \$one)\n\n    @foreach(\$twos as \$two)\n\n        multiline content\n\n    @endforeach\n\n@endforeach\n\n@foreach(\$three as \$three)\n\n    @other_expression\n\n    @end_other_expression\n\n@endforeach>"; 
   $result = preg_replace("/\\@endforeach/", "<?php } ?>", preg_replace("/\\@foreach(.*)/", "<?php foreach$1 { ?>", $str));
   print $result;
?>

输出:

<?php foreach(($ones) as $one) { ?>                                                                                                                                                                                                                    

    <?php foreach($twos as $two) { ?>                                                                                                                                                                                                                  

        multiline content                                                                                                                                                                                                                              

    <?php } ?>                                                                                                                                                                                                                                         

<?php } ?>                                                                                                                                                                                                                                             

<?php foreach($three as $three) { ?>                                                                                                                                                                                                                   

    @other_expression                                                                                                                                                                                                                                  

    @end_other_expression                                                                                                                                                                                                                              

<?php } ?>