title: Excessive spaces caused by BBcode tables (PHP)
body: <p>I've done extensive Googling, and I just can't seem to figure it out. Hopefully you can help me.</p>
  
  <p>I'm programming a message board. The system filters HTML out of replies, and forces my members to use standard BBcode instead.  All of the basic BBcodes are running smoothly, however I've run into a snag with my table codes.</p>
  
  <p><strong>The BBcode</strong></p>
  
  <p>This is the script that controls the BBcode on my message board. It's pretty simple and straight forward. I'm concerned about [table] [tr] and [td] only, but I included [b] so that you will understand that the function handles more than just the three codes I'm working with.</p>
  
  <pre><code>function BBcode($original_string) {
  
  $find = array(
  '/\[b\](.*?)\[\/b\]/is',
  '/\[table\](.*?)\[\/table\]/is',
  '/\[tr\](.*?)\[\/tr\]/is',
  '/\[td\](.*?)\[\/td\]/is'
  );
  
  $replace = array(
  '&lt;b&gt;$1&lt;/b&gt;',
  '&lt;table border="1" cellpadding="5"&gt;$1&lt;/table&gt;',
  '&lt;tr&gt;$1&lt;/tr&gt;',
  '&lt;td&gt;$1&lt;/td&gt;
  );
  
  $new_string = preg_replace ($find, $replace, $original_string);
  
  return $new_string;
  
  }
  </code></pre>
  
  <p>So if a member wanted to use table BBcode codes in their response on the message boards, they would probably type something like this:</p>
  
  <pre><code>[table]
  
  [tr]
  [td]Cell 1[/td]
  [td]Cell 2[/td]
  [td]Cell 3[/td]
  [/tr]
  
  [tr]
  [td]x[/td]
  [td]y[/td]
  [td]z[/td]
  [/tr]
  
  [/table]
  </code></pre>
  
  <p>Which is great! This works just as planned. </p>
  
  <p><strong>The Problem</strong></p>
  
  <p>Though it has one major flaw &ndash; if a member submits the table using the BBcode I provided above, nl2br adds a whole bunch of useless spaces. You can see what I mean here: </p>
  
  <p><img src="https://i.imgur.com/DviUzoM.png" alt=""></p>
  
  <p>(imagine how much worse it will be with each added TR)</p>
  
  <p>However, if the user posts their comment like this: </p>
  
  <pre><code>[table] [tr] [td]Cell 1[/td] [td]Cell 2[/td] [td]Cell 3[/td] [/tr] [tr] [td]x[/td] [td]y[/td] [td]z[/td] [/tr] [/table]
  </code></pre>
  
  <p>Their posted comment looks fine: </p>
  
  <p><img src="https://i.imgur.com/1sTFN6n.png" alt=""></p>
  
  <p><strong>What I've Tried Already</strong></p>
  
  <p>I've figured out that this is happening because I use nl2br on these comments. Currently, it adds BR tags before the BBcode function runs. I tried moving nl2br after the BBcode, but that didn't work.</p>
  
  <p>Next, I went back to the BBcode function and tried to do an str_replace within the arrays. </p>
  
  <pre><code>function BBcode($original_string) {
  
  $find = array(
  '/\[b\](.*?)\[\/b\]/is',
  '/\[table\](.*?)\[\/table\]/is',
  '/\[tr\](.*?)\[\/tr\]/is',
  '/\[td\](.*?)\[\/td\]/is'
  );
  
  $replace = array(
  '&lt;b&gt;$1&lt;/b&gt;',
  '&lt;table border="1" cellpadding="5"&gt;' .print str_replace("&lt;br /&gt;", "", "$1"). '&lt;/table&gt;',
  '&lt;tr&gt;$1&lt;/tr&gt;',
  '&lt;td&gt;$1&lt;/td&gt;
  );
  
  $new_string = preg_replace ($find, $replace, $original_string);
  
  return $new_string;
  
  }
  </code></pre>
  
  <p>I also tried to write that line countless other ways, such as:</p>
  
  <pre><code>'&lt;table border="1" cellpadding="5"&gt;' .str_replace("&lt;br /&gt;", "", "$1"). '&lt;/table&gt;',
  '&lt;table border="1" cellpadding="5"&gt;' .str_replace("&lt;br /&gt;", "", "$1"). '$1&lt;/table&gt;',
  '&lt;table border="1" cellpadding="5"&gt;' .str_replace("&lt;br /&gt;", "", "$1"). '$1&lt;/table&gt;',
  '&lt;table border="1" cellpadding="5"&gt;' . $thing = str_replace("&lt;br /&gt;", "", "$1"); . '$thing&lt;/table&gt;',
  '&lt;table border="1" cellpadding="5"&gt;' . $thing = str_replace("&lt;br /&gt;", "", "$1") . '$thing&lt;/table&gt;',
  '&lt;table border="1" cellpadding="5"&gt;' . print str_replace("&lt;br /&gt;", "", $1); . '&lt;/table&gt;',
  etc
  </code></pre>
  
  <p>Basically, what I want to do is replace all of the BR tags that are found in the [table] code. How do I do this? What am I doing wrong? Should I change my approach?</p>
  
  <p>I apologize in advance if this question has been answered elsewhere. I saw at least three people with this same issue on other forums, but the OPs were all linked to the same place &ndash; a thread with the solution now given on an expired link. But if I missed something, please link me!</p>
  
  <p>If you have any questions, please ask!</p>