title: Regular Expression: Converting non-block elements with <br /> to <p> in PHP
body: <p>Someone has asked a <a href="https://stackoverflow.com/questions/2959734/php-replacing-double-br-with-pp">similar question</a>, but the accepted answer doesn't meet my requirements. </p>
  
  <p><strong>Input:</strong>
  </p>
  
  <pre><code>&lt;strong&gt;bold &lt;br /&gt;&lt;br /&gt; text&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;a href="#"&gt;link&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;pre&gt;some code&lt;/pre&gt;
  I'm a single br, &lt;br /&gt; leave me alone.
  </code></pre>
  
  <p><strong>Expected output:</strong></p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;p&gt;&lt;strong&gt;bold &lt;br /&gt; text&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;
  &lt;p&gt;&lt;a href="#"&gt;link&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;
  &lt;pre&gt;some code&lt;/pre&gt;
  &lt;p&gt;I'm a single br, &lt;br /&gt; leave me alone.&lt;/p&gt;
  </code></pre>
  
  <p>The accepted answer I mentioned above will convert multiple br to p, and at last wrap all the input with another p. But in my case, you can't wrap pre inside a p tag. Can anyone help?</p>
  
  <h2>update</h2>
  
  <p>the expected output before this edit was a little bit confusing. the whole point is:</p>
  
  <ol>
  <li><p>convert multiple br to a single one (achieved with <code>preg_replace('/(&lt;br />)+/', '&lt;br />', $str);</code>)</p></li>
  <li><p>check for inline elements and unwrapped text (there's no parent element in this case, input is from $_POST) and wrap with &lt;p>, leave block level elements alone.</p></li>
  </ol>