title: How to replace string dynamically?
body: <p>I have a string like this</p>
  
  <pre><code>{columns}
  {col}
    Column 1
  {/col}
  
  {col}
     Column 2
  {/col}
  {/columns}
  
  ...    
  
  {columns}
  {col}
    Column 1
  {/col}
  
  {col}
    Column 2
  {/col}
  {col}
    Column 3
  {/col}
  {/columns}
  
  // Result
  &lt;div class="row"&gt;
   &lt;div class="col-md-6"&gt;
     ...
   &lt;/div&gt;
   &lt;div class="col-md-6"&gt;
     ...
   &lt;/div&gt;
  &lt;/div&gt;
  ...
  </code></pre>
  
  <p>I want replace the <code>{col}</code> with <code>&lt;div class="col-md-x&gt;&lt;/div&gt;"</code>. It should be <strong>dynamical</strong>, that's mean if a columns has three cols, the class should be <code>col-md-4</code>. The length of col is 12. I'm trying with <code>preg_replace</code> or <code>preg_replace_all</code>, but it doesn't work really. 
  I can only replace the <code>{columns}</code> with </p>
  
  <pre><code>preg_replace("/{columns}(.+?){\/columns}/is", "&lt;div class='row'&gt;$1&lt;/div&gt;", $text); 
  </code></pre>
  
  <p>but the other stuff is to difficult. Can someone help me, thx in advance</p>
  
  <p>here is an example
  <a href="https://3v4l.org/mPrMS" rel="nofollow noreferrer">https://3v4l.org/mPrMS</a></p>