title: Ignore second match's HTML if not matched
body: <p>I'm writing a regular expression that should do the following:</p>
  
  <pre><code>== Text ==
  Other text
  ==     Text==
  </code></pre>
  
  <p>Becomes</p>
  
  <pre><code>&lt;h2&gt;Text&lt;/h2&gt;
  &lt;p&gt;Other text&lt;/p&gt;
  &lt;h2&gt;Text&lt;/h2&gt;
  </code></pre>
  
  <p>I'm almost there, the problem is that this is what I currently get:</p>
  
  <pre><code>&lt;h2&gt;Text&lt;/h2&gt;
  &lt;p&gt;Other text&lt;/p&gt;
  &lt;h2&gt;Text&lt;/h2&gt;
  &lt;p&gt;&lt;/p&gt;
  </code></pre>
  
  <p>Even though it's unlikely the heading will not be followed by text, I want to fix it at least for learning purposes.</p>
  
  <p>Here is my function:</p>
  
  <pre><code>preg_replace('/== *(.*?) *==([^=]*)/m', 
               '&lt;h2&gt;$1&lt;/h2&gt;
                &lt;p&gt;$2&lt;/p&gt;
               ', '== Text ==
                   Other text
                   ==     Text==');
  </code></pre>
  
  <p>So basically, I want to ignore the <code>&lt;p&gt;&lt;/p&gt;</code> part if <code>$2</code> is empty.</p>
  
  <p>Any other tips / improvements are welcomed, I want to learn :)</p>