Regex matches double curly braces

will the regex match while double curly brackets / loop contain double curly braces?

<?php $str = '<html lang="{{var doc-lang}}"> <head> <title>{{var doc-title}}</div> </head> <body> <div class="container"> <div class="row" {{while products}}> {{var name}} {{var sku}} {{var barcode}} </div {{while end}}> </div> </body> </html>'; 

Can we get

 <div class class="row"', [products], {{var name}}, {{ var sku}} and {{var barcode}} 

from $str ?

I can only think about it Reg

 \<((.*)\{\{while\s+(.*)\}\}(.*)\{\{while\s+end\}\})\> 

Regex101

0
source share
1 answer

Try the following:

 /\s*(.*\{\{while\s+.*(?:\r?\n.*)+\{\{while\s+end.*)/ 

Online demo


Also, to get them separately (as you mentioned in the question and comments), try the following:

 /\s*(?:(.*)\s+\{\{while\s+(.*)\}\}.*\r?\n\s*(.*)\r?\n\s*(.*)\r?\n\s*(.*)\r?\n.*)/ 

And you can get what you need:

 '$1', [$2], $3, $4 and $5 

Output:

 '<div class class="row"', [products], {{var name}}, {{ var sku}} and {{var barcode}} 

Online demo

+2
source

All Articles