0

would regex matches while/loop double curly braces contains 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 only can think of this Reg

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

Regex101

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
meYnot
  • 343
  • 2
  • 6
  • 13
  • Sorry, {{var=doc-lang}}, {{var=doc-title}} does not have '=' sign, replace with empty space. – meYnot Feb 19 '16 at 13:01
  • Obviously you are trying to come up with your own templating system. There are plenty of flexible systems out there already available, Twig is exceptional http://twig.sensiolabs.org – zanderwar Feb 19 '16 at 13:09
  • @Zanderwar, that's correct, but we are doing single-file applications, we can not afford an add-on classes. – meYnot Feb 19 '16 at 18:14
  • Fair enough :) Best of luck – zanderwar Feb 24 '16 at 07:40

1 Answers1

2

Try this:

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

Online Demo


Also to get them separately (as you mentioned in the question and comment), Try this:

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

And you can get what you need like this:

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

Output:

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

Online Demo

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • Thanks @Shafizadeh that is almost right, but can I get them separated not in one block? – meYnot Feb 19 '16 at 13:44
  • Thanks a lot, that solves the issue I am raising, but on the other hand my {{if var sku > 0 }} {{var sku}} {{end if}} regex is not functioning no more with this solution, kindly help get the IF block. – meYnot Feb 19 '16 at 18:19