arkhon
arkhon

Reputation: 765

preg replace in php with custom tags

I want to parse a custom template file and still struggle with regular expressions.

I want to parse the following file:

@foreach(($ones) as $one)

    @foreach($twos as $two)

        multiline content

    @endforeach

@endforeach

@foreach($three as $three)

    @other_expression

    @end_other_expression

@endforeach

The result should be:

<?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 } ?>

Replacing the @endforeach is rather easy. I did it with this code:

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

Now i need to replace the @foreach part which i tried with the following code:

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

The problem is that this pattern does not recognize the end of my @foreach() statement. The \n for a new line does not work. I cant use the closing bracket either because it is possible that there are multiple brackets inside the foreachhead.

Im open for any suggestion.

Thanks in advance.

Upvotes: 0

Views: 229

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626801

You can use 2 regexes at a row to do this:

<?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;
?>

Output:

<?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 } ?>

Upvotes: 2

Related Questions