0

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.

2
  • It's not exactly HTML, but you should not use RegEx to parse markup in general. It's slow and difficult to maintain. Commented Apr 3, 2015 at 8:26
  • Whats the alternative? Commented Apr 3, 2015 at 8:46

1 Answer 1

2

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 } ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.