title: PHP regex crashing apache
body: <p>I've got a regex that does matching for a template system, which unfortunately seems to crash apache (it's running on Windows) on some modestly-trivial lookups.  I've researched the issue and there are a few suggestions for upping stack size etc, none of which seem to work and I don't really like dealing with such issues by upping limits anyway as it generally just pushed the bug into the future.</p>
  
  <p>Anyway any ideas on how to alter the regex to make it less likely to foul up?</p>
  
  <p>The idea is to catch the innermost block (in this case <code>{block:test}This should be caught first!{/block:test}</code>) which I'll then str_replace out the starting/ending tags and re-run the whole thing through the regex until there are no blocks left.</p>
  
  <p>Regex:</p>
  
  <pre><code>~(?P&lt;opening&gt;{(?P&lt;inverse&gt;[!])?block:(?P&lt;name&gt;[a-z0-9\s_-]+)})(?P&lt;contents&gt;(?:(?!{/?block:[0-9a-z-_]+}).)*)(?P&lt;closing&gt;{/block:\3})~ism
  </code></pre>
  
  <p>Sample template:</p>
  
  <pre><code>&lt;div class="f_sponsors s_banners"&gt;
      &lt;div class="s_previous"&gt;&amp;laquo;&lt;/div&gt;
      &lt;div class="s_sponsors"&gt;
          &lt;ul&gt;
              {block:sponsors}
              &lt;li&gt;
                  &lt;a href="{var:url}" target="_blank"&gt;
                      &lt;img src="image/160x126/{var:image}" alt="{var:name}" title="{var:name}" /&gt;
                  &lt;/a&gt;
              {block:test}This should be caught first!{/block:test}
              &lt;/li&gt;
              {/block:sponsors}
          &lt;/ul&gt;
      &lt;/div&gt;
      &lt;div class="s_next"&gt;&amp;raquo;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>It's a long shot I suppose.  :(</p>