php中的preg替换为自定义标记


preg replace in php with custom tags

我想解析一个自定义模板文件,但仍然要处理正则表达式。

我想解析以下文件:

@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无效。我也不能使用右括号,因为foreahead内部可能有多个括号。

我愿意接受任何建议。

提前谢谢。

您可以在一行中使用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 } ?>