title: Converting <br /> into <p> tag for standardize a text
body: <p>I have a HTML that is in various formats and I'm trying to standardize the code using PHP. In some cases I receive it using breaks <br> to separate lines. I need these to be <code>&lt;p&gt;</code> tags instead.</p>
  
  <p>1- For fist step I try to split the text by <code>&lt;br&gt;</code> and add it to new holder</p>
  
  <pre><code>&lt;?php
  
  $text = 'I am &lt;br&gt;&lt;br&gt;       &lt;br&gt;&lt;p&gt; &lt;br&gt;your firnd &lt;br&gt;&lt;/p&gt;&lt;p&gt;   &lt;/p&gt; ok?';
  $breaks = array("&lt;br /&gt;","&lt;br&gt;","&lt;br/&gt;","&lt;p&gt;","&lt;/p&gt;");  
  $text = str_ireplace($breaks, "&lt;br&gt;", $text); 
  echo "R1: ".htmlspecialchars($text, ENT_QUOTES, "UTF-8");
  $text = explode("&lt;br&gt;", $text);
  $newText = null;
  for($i=0; $i &lt; count($text) ;$i++)
  {
      if(!empty($text[$i]))
          $newText .= "&lt;p&gt;".$text[$i]."&lt;/p&gt;";
  }
  
  
  $breaks = array("&lt;p&gt;&lt;/p&gt;","&lt;p&gt; &lt;/p&gt;","&lt;p&gt; &lt;/p&gt;");  
  
  echo "&lt;br/&gt;R2: ".htmlspecialchars($newText, ENT_QUOTES, "UTF-8");
  
  $newText = str_ireplace($breaks, "", $newText); 
  
  echo "&lt;br/&gt;R3: ".htmlspecialchars($newText, ENT_QUOTES, "UTF-8");
  ?&gt;
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>R1: I am &lt;br&gt;&lt;br&gt; &lt;br&gt;&lt;br&gt; &lt;br&gt;your firnd &lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;br&gt; ok?
  R2: &lt;p&gt;I am &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;your firnd &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt; ok?&lt;/p&gt;
  R3: &lt;p&gt;I am &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;your firnd &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt; ok?&lt;/p&gt;
  </code></pre>
  
  <p>Do you a suggestion for me to improve this code?
  Also I have 2 issue whit that...</p>
  
  <p>And how can avoid generate extra <code>&lt;p&gt;&lt;/p&gt;</code> in my code and also how can remove attributes of p (id,class,meta,etc...) in p tag?</p>
  
  <p>for example for this example:</p>
  
  <pre><code>$text = 'I am &lt;br&gt;&lt;br&gt;       &lt;br&gt;&lt;p id="ids" class="classes" meta-name="test"&gt; &lt;br&gt;your firnd &lt;br&gt;&lt;/p&gt;&lt;p&gt;   &lt;/p&gt; ok?';
  </code></pre>