Inserting line breaks except inside table tags

$myVar='This text is the first line before the table tags.
This text is the second line before the table tags.
<table>
  <tr>
    <td class="borderLine">
      This text is the first line inside the table tags.
      This text is the second line inside the table tags.
    </td>
     <td>
        This text is in the second cell of the tagle tags.
     </td>
  </tr>
   <tr class="className">
     <td>
      This text is in the first cell of the second row.
    </td>
    <td>
      This text is in the second cell of the second row.
    </td>
  </tr>
</table>
This text is the first line after the table tags.
This text is the second line after the table tags.';


I like to insert line breaks into $myVar per line.
The code below makes the result1 below.


$myVar1=nl2br($myVar);

[b]result1[/b]

This text is the first line before the table tags.[COLOR="#0000CD"]<br />[/COLOR]
This text is the second line before the table tags.[COLOR="#0000CD"]<br />[/COLOR]
<table>[COLOR="#FF0000"]<br />[/COLOR]
  <tr>[COLOR="#FF0000"]<br />[/COLOR]
    <td class="borderLine">[COLOR="#FF0000"]<br />[/COLOR]
      This text is the first line inside the table tags.[COLOR="#0000CD"]<br />[/COLOR]
      This text is the second line inside the table tags.[COLOR="#0000CD"]<br />[/COLOR]
    </td>[COLOR="#FF0000"]<br />[/COLOR]
     <td>[COLOR="#FF0000"]<br />[/COLOR]
        This text is in the second cell of the tagle tags.[COLOR="#0000CD"]<br />[/COLOR]
     </td> [COLOR="#FF0000"]<br />[/COLOR]
  </tr>[COLOR="#FF0000"]<br />[/COLOR]
  <tr class="className">[COLOR="#FF0000"]<br />[/COLOR]
    <td><br />
      This text is in the first cell of the second row. [COLOR="#0000CD"]<br />[/COLOR]
    </td>[COLOR="#FF0000"]<br />[/COLOR]
    <td>[COLOR="#FF0000"]<br />[/COLOR]
      This text is in the second cell of the second row. [COLOR="#0000CD"]<br />[/COLOR]
    </td>[COLOR="#FF0000"]<br />[/COLOR]
  </tr>[COLOR="#FF0000"]<br />[/COLOR]
</table>[COLOR="#0000CD"]<br />[/COLOR]
This text is the first line after the table tags.[COLOR="#0000CD"]<br />[/COLOR]
This text is the second line after the table tags.

The <br />s in blue are okay, but the <br />s in red produce problems in the result1 above.

The code below makes the result2 below.


$myVar2=str_replace('<table><br />','<table>',$myVar1);
$myVar2=str_replace('</table><br />','</table>',$myVar2);
$myVar2=str_replace('<tr><br />','<tr>',$myVar2);
$myVar2=str_replace('</tr><br />','</tr>',$myVar2);
$myVar2=str_replace('<td><br />','<td>',$myVar2);
$myVar2=str_replace('</td><br />','</td>',$myVar2);

[b]result2[/b]

This text is the first line before the table tags.[COLOR="#0000CD"]<br />[/COLOR]
This text is the second line before the table tags.[COLOR="#0000CD"]<br />[/COLOR]
<table>
  <tr>
    <td class="borderLine">[COLOR="#FF0000"]<br />[/COLOR]
      This text is the first line inside the table tags.[COLOR="#0000CD"]<br />[/COLOR]
      This text is the second line inside the table tags.[COLOR="#0000CD"]<br />[/COLOR]
    </td>
     <td>
        This text is in the second cell of the tagle tags.[COLOR="#0000CD"]<br />[/COLOR]
     </td>
  </tr>
  <tr class="className">[COLOR="#FF0000"]<br />[/COLOR]
    <td>
      This text is in the first cell of the second row. [COLOR="#0000CD"]<br />[/COLOR]
    </td>
    <td>
      This text is in the second cell of the second row. [COLOR="#0000CD"]<br />[/COLOR]
    </td>
  </tr>
</table>[COLOR="#0000CD"]<br />[/COLOR]
This text is the first line after the table tags.[COLOR="#0000CD"]<br />[/COLOR]
This text is the second line after the table tags.

Now I have two problem making <br /> in red.
They are not removed by str_replace() because they have class.
Since the class name is varied I can’t use str_replace() for the all class names.

How can I remove the <br />s inside the table tags?

(
There is no colspan, rowspan or style inside the <tr> or <td> tag.
There are just a variety of class names.

I can’t use the following code because there might be other tag outside of the table tag. for example <span class=“borderLine”>span text</span>.

$myVar3=str_replace('[COLOR="#FF0000"]"><br />[/COLOR]','[COLOR="#FF0000"]">[/COLOR]',$myVar2);

)

Is it true that you only want a <br /> to be entered when a newline character is preceded by a dot (.) ?

Or is that mere coincidence because of the way you structured your example?


This text is the second line before the table tags.<br />
<table>
  <tr>
    <td class="borderLine">
      This text is the first line inside the table tags.<br />
      This text is the second line inside the table tags.<br />
... and so on