Good all the time of day, there was some problem ...

The following line is stored in the database:

<div class="lol">{foreach(message)}<div class="123">{message.user}</div><div>{message.text}</div>{endforeach}</div> 

When we make a request to display this content, before giving an answer, we parse it.

The question of how to make it so that from this: {foreach(message)} is this: <?=foreach($parent as $message);?>

Simply put, to get such source code

 <div class="lol"><? foreach($parent as $message) ?><div class="123"><?=$message['user'];?></div><div><?=$message['text'];?></div><?=endforeach;?></div> 
  • 3
    This is not "PHP parsing", but parsing something else. We will understand what is “other” (some template maker?) - we will understand how it is better. For now - the specific problem is solved head-on with the simplest regexp. - PinkTux
  • Well, okay, reqexp does not solve this, since it is not only necessary to replace the specific one, but to check whether the instructions are valid, and the @PinkTux application that is not working. In general, it looks like a blade pattern. - Naumov
  • You very carelessly read my comment. It dealt with a specific task (in the wording of which there was not a word about validation or code variability - there was a specific line). If you need all the buns, starting with parsing different lines and ending with validation, write a normal parser. And even easier - modify the ready one from the template engine, since it is known. - PinkTux
  • This is a self-written code. The content itself is stored in the database, respectively, when we make a request to issue certain content, we must first convert this kind of message.user code into php, that is, for example, there is a function, parseHtml () {}, it takes 2 parameters $ params (Maybe as an array or with 1 input) and $ content itself, that is, parseHtml (array $ params = array (), $ content) Next, in the content, we search for all strings that contain {$ 1} data That is in content we have {message.user} - DimaRRR
  • We replace this line as follows:

0