SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    inserting line breaks except inside table tags

    Code:
    $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.
    Code:
    $myVar1=nl2br($myVar);
    
    result1
    
    This text is the first line before the table tags.<br />
    This text is the second line before the table tags.<br />
    <table><br />
      <tr><br />
        <td class="borderLine"><br />
          This text is the first line inside the table tags.<br />
          This text is the second line inside the table tags.<br />
        </td><br />
         <td><br />
            This text is in the second cell of the tagle tags.<br />
         </td> <br />
      </tr><br />
      <tr class="className"><br />
        <td><br />
          This text is in the first cell of the second row. <br />
        </td><br />
        <td><br />
          This text is in the second cell of the second row. <br />
        </td><br />
      </tr><br />
    </table><br />
    This text is the first line after the table tags.<br />
    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.
    Code:
    $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);
    
    result2
    
    This text is the first line before the table tags.<br />
    This text is the second line before the table tags.<br />
    <table>
      <tr>
        <td class="borderLine"><br />
          This text is the first line inside the table tags.<br />
          This text is the second line inside the table tags.<br />
        </td>
         <td>
            This text is in the second cell of the tagle tags.<br />
         </td> 
      </tr>
      <tr class="className"><br />
        <td>
          This text is in the first cell of the second row. <br />
        </td>
        <td>
          This text is in the second cell of the second row. <br />
        </td>
      </tr>
    </table><br />
    This text is the first line after the table tags.<br />
    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>.

    Code:
    $myVar3=str_replace('"><br />','">',$myVar2);
    )

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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?

    PHP Code:
    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 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •