REGEX to get information inside a string

0

I'm studying PHP OO and part of my study is in putting together a Class that works a template engine. I've converted the file to a string with file_get_contents and now I want to get a part of that string to generate a LOOP.

{LOOP}
   <option value="%SEL-VL%">%SEL-TXT%</option>
{ENDLOOP}

My idea is to use preg_match to get the content that is within {LOOP} and {ENDLOOP} and make substitutions for the variables by repeating the html block that was taken.

I need help creating the REGEX that I will use in preg_macth. Can someone give me this strength?

I accept ideas too ... NOTE: I do not use a ready-made framework or the ready-made template engine available on the internet, as this development is part of my study and practice of PHP OO.

Thank you all.

    
asked by anonymous 10.01.2018 / 05:28

1 answer

1

As far as I understand, your question qualifies as Lexer or Interpreter.

However you have your interval well defined, although you do not know exactly how you will treat it later.

You can use Regex:

({LOOP})(.*?)({ENDLOOP})

Explanation

  • ({LOOP}) - Group {LOOP} - Identifies the start of the catch.
  • ({ENDLOOP}) - Group {ENDLOOP} - Identifies the end of the catch.
  • (.*?) - Content Group - The least possible not to have problem with another loop.

Be Regex101 .

Problem

Because it is an interpreter you may end up having the following problem in using Regex.

Solution

The ideal would be to work with recursive conversions, going from the inside out, once you have everything converted, just do the opposite to mount the final string .

    
10.01.2018 / 12:31