Removing Line Returns While Keeping HTML

Discussion in 'PHP' started by aaron_nimocks, Jan 6, 2010.

  1. #1
    I need to format a bunch of HTML into 1 line.

    Example

    <TBODY>
    <TR>
    <TH>Poids </TH>
    <TD>9 KG/UN </TD></TR></TBODY>
    HTML:
    Now when I run the following code it works for normal characters but it doesn't work with HTML tags.

    <?php 
    $string = preg_replace( "{\s+}", ' ', $string );
    echo $string;
    ?>
    PHP:
    Desired output would be.

    <TBODY><TR><TH>Poids </TH><TD>9 KG/UN </TD></TR></TBODY>
    HTML:
     
    aaron_nimocks, Jan 6, 2010 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #2
    Try this

    <?php
    
    $str = "<TBODY>
    <TR>
    <TH>Poids </TH>
    <TD>9 KG/UN </TD></TR></TBODY>";
    
    $str = preg_replace_callback('~\<[^>]+\>.*\</[^>]+\>~ms','stripNewLines', $str); 
    
    function stripNewLines($match) {
        return str_replace(array("\r", "\n"), '', $match[0]);   
    }
    
    echo nl2br($str);
    ?>
    Code (markup):
     
    Bohra, Jan 6, 2010 IP
  3. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #3
    Thanks, but that removed the HTML tags too.

    I just want to make the HTML all one line so it can go in a CSV file.

    Output would look like...

     
    aaron_nimocks, Jan 6, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Here you go; no need to use regular expressions:

    <?php
    
    $html = <<<HTML
    <TBODY>
    <TR>
    <TH>Poids </TH>
    <TD>9 KG/UN </TD></TR></TBODY>
    HTML;
    
    $html = str_replace("\n","", $html);
    
    highlight_string($html);
    
    ?>
    PHP:
     
    danx10, Jan 6, 2010 IP
  5. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #5
    Thanks guess highlight string is what I was missing.

    Still got this stray "1" character though. Makes no sense on where it is coming from.

    http://www.ubottutorials.com/newline.php

    <form id="form1" name="form1" method="post" action="newline.php">
      <label>Input :</label>
      <textarea name="input2" id="input2" cols="45" rows="5"></textarea>
        <br />
        <input type="submit" name="submit" id="submit" value="Submit" />
      </p>
      </form>
    
    <?php 
    
    $string = $_POST["input2"];
    $string = preg_replace( "{\s+}", ' ', $string );
    
    echo highlight_string($string);
    ?>
    PHP:
     
    aaron_nimocks, Jan 6, 2010 IP
  6. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #6
    oh ic u wanted the output on the browser to be like that i posted it for source code i guess u got the code from the above guy now
     
    Bohra, Jan 6, 2010 IP
    aaron_nimocks likes this.
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Aaron, you don't use echo() with highlight_string(), remove the echo() and use the following:

    <form id="form1" name="form1" method="post" action="newline.php">
      <label>Input :</label>
      <textarea name="input2" id="input2" cols="45" rows="5"></textarea>
        <br />
        <input type="submit" name="submit" id="submit" value="Submit" />
      </p>
      </form>
    
    <?php 
    
    $string = $_POST["input2"];
    $string = preg_replace( "{\s+}", ' ', $string );
    
    highlight_string($string);
    ?>
    PHP:
     
    danx10, Jan 6, 2010 IP
    aaron_nimocks likes this.
  8. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #8
    Opps. Thanks it works. :)
     
    aaron_nimocks, Jan 6, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Sure; no problem. You might also want to add (if you havent already). An if statement to verify if the forms been submitted, that way you can contain the printed code to only print when the form is submitted. :)

    <form id="form1" name="form1" method="post" action="newline.php">
      <label>Input :</label>
      <textarea name="input2" id="input2" cols="45" rows="5"></textarea>
        <br />
        <input type="submit" name="submit" id="submit" value="Submit" />
      </p>
      </form>
    
    <?php 
    if(isset($_POST['submit'])) {
    $string = $_POST["input2"];
    $string = preg_replace( "{\s+}", ' ', $string );
    
    highlight_string($string);
    }
    ?>
    PHP:
     
    danx10, Jan 6, 2010 IP