title: How to preserve whitespace with some restrictions with php?
body: <p>I am trying to build an site where people write and post it to the database. The text is written in <code>&lt;textarea&gt;</code> and I want to preserve the whitespaces as formatting.</p>
  <p>For example, users have to press <code>ENTER</code> to get to a new line from the current one they were typing on, after that, if they want to press <code>ENTER</code> again by writing nothing, that means there will be two new lines, I want to keep the two or more than two new lines restricted to just one blank line.</p>
  <p>Stack Overflow has this feature, while writing this line, I pressed enter thrice from the last line, but you can only see one white space.</p>
  <p><a href="https://i.stack.imgur.com/3gueQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3gueQ.png" alt="As I pressed enter 3 times, three &lt;br&gt; were created resulting in three white spaces" /></a></p>
  <p>How do I achieve this with PHP? I have tried <code>nl2br()</code> but that seems to change every <code>\n</code> to <code>&lt;br \&gt;</code>. How do I solve this issue?</p>
  <p><strong>The actual source code:</strong></p>
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
  &lt;textarea name=&quot;ab&quot;&gt;
  &lt;/textarea&gt;
  &lt;button&gt;Sub&lt;/button&gt;&lt;/form&gt;
  &lt;?php
  
  $post= preg_replace('/\n+/', &quot;\n&quot;, $_POST['ab']);
  echo nl2br($post);
   
   //echo str_replace($find,$replace,$post);
  
   
   ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>This is the output html source:</strong></p>
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
  &lt;textarea name=&quot;ab&quot;&gt;
  &lt;/textarea&gt;
  &lt;button&gt;Sub&lt;/button&gt;&lt;/form&gt;
  This is a single line.&lt;br /&gt;
  now I pressed enter,&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  three spaces below (should show only 1 whitespace)&lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Look at the <code>&lt;br /&gt;</code> created by the <code>nl2br</code>, I want the consecutive <code>&lt;br /r&gt;</code> to be <code>&lt;p&gt;...&lt;/p&gt;</code>.</p>
  <p><strong>This is what I want to be the output html source:</strong></p>
  <pre><code>&lt;!doctype html&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;title&gt;Untitled Document&lt;/title&gt;
      &lt;/head&gt;
      
      &lt;body&gt;
      &lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
      &lt;textarea name=&quot;ab&quot;&gt;
      &lt;/textarea&gt;
      &lt;button&gt;Sub&lt;/button&gt;&lt;/form&gt;
      This is a single line.&lt;br /&gt;
      now I pressed enter,&lt;br /&gt;
      &lt;p&gt;
      three spaces below (should show only 1 whitespace)&lt;/p&gt;&lt;/body&gt;
      &lt;/html&gt;
  </code></pre>