Remove whitespace from HTML

前端 未结 15 1724
花落未央
花落未央 2020-12-28 14:25

I have HTML code like:

相关标签:
15条回答
  • 2020-12-28 14:33

    This works for me and it's easy to add/remove special cases. Works with CSS, HTML and JS.

    function inline_trim($t)
    {
        $t = preg_replace('/>\s*\n\s*</', '><', $t); // line break between tags
        $t = preg_replace('/\n/', ' ', $t); // line break to space
        $t = preg_replace('/(.)\s+(.)/', '$1 $2', $t); // spaces between letters
        $t = preg_replace("/;\s*(.)/", ';$1', $t); // colon and letter
        $t = preg_replace("/>\s*(.)/", '>$1', $t); // tag and letter
        $t = preg_replace("/(.)\s*</", '$1<', $t); // letter and tag
        $t = preg_replace("/;\s*</", '<', $t); // colon and tag
        $t = preg_replace("/;\s*}/", '}', $t); // colon and curly brace
        $t = preg_replace("/(.)\s*}/", '$1}', $t); // letter and curly brace
        $t = preg_replace("/(.)\s*{/", '$1{', $t); // letter and curly brace
        $t = preg_replace("/{\s*{/", '{{', $t); // curly brace and curly brace
        $t = preg_replace("/}\s*}/", '}}', $t); // curly brace and curly brace
        $t = preg_replace("/{\s*([\w|.|\$])/", '{$1', $t); // curly brace and letter
        $t = preg_replace("/}\s*([\w|.|\$])/", '}$1', $t); // curly brace and letter
        $t = preg_replace("/\+\s+\'/", "+ '", $t); // plus and quote
        $t = preg_replace('/\+\s+\"/', '+ "', $t); // plus and double quote
        $t = preg_replace("/\'\s+\+/", "' +", $t); // quote and plus
        $t = preg_replace('/\"\s+\+/', '" +', $t); // double quote and plus
    
        return $t;
    }
    
    0 讨论(0)
  • 2020-12-28 14:34

    The array reduce function:

    $html = explode("\n", $html);
    function trimArray($returner, $value) {
        $returner .= trim($value);
        return $returner;
    }
    echo $html = array_reduce($html, 'trimArray');
    
    0 讨论(0)
  • 2020-12-28 14:34

    I used this regex for me and it works like a charm:

    preg_replace('/[ \t]+(?!="|\')/', '', $html);
    

    These pattern looks for space whitespace and tabulator (at least one), that is not followed by " or '. This is, to avoid removing whitespaces between html attributes.

    0 讨论(0)
  • 2020-12-28 14:35

    Just in case someone still needs this, I coined a function from @Martin Angelova's response and @Savas Vedova, the outcome that also solved my problem looks:

    <?php 
       function rmspace($buffer){ 
            return preg_replace('~>\s*\n\s*<~', '><', $buffer); 
       };
    ?>
    <?php ob_start("rmspace");  ?>
       //Content goes in here 
    <?php ob_end_flush(); ?>
    

    Note: I did not test the performance penalty in a production environment

    0 讨论(0)
  • 2020-12-28 14:38
    $html = preg_replace('~>\s*\n\s*<~', '><', $html);
    

    I'm thinking that this is the solution to the <b>Hello</b> <i>world</i> problem. The idea is to remove whitespace only when there's a new line. It will work for common HTML syntax which is:

    <div class="wrap">
        <div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-28 14:45

    It's been a while since this question was first asked but I still see the need to post this answer in order to help people with the same problem.

    None of these solutions were adoptabe for me therefore I've came up with this solution: Using output_buffer.

    The function ob_start accepts a callback as an argument which is applied to the whole string before outputting it. Therefore if you remove whitespace from the string before flushing the output, there you're done.

    /** 
     * Remove multiple spaces from the buffer.
     * 
     * @var string $buffer
     * @return string
     */
    function removeWhitespace($buffer)
    {
        return preg_replace('/\s+/', ' ', $buffer);
    }
    
    ob_start('removeWhitespace');
    
    <!DOCTYPE html>
    <html>
        <head></head>
        <body></body>
    </html>
    
    ob_get_flush();
    

    The above would print something like:

    <!DOCTYPE html> <html> <head> </head> <body> </body> </html>
    

    Hope that helps.

    HOW TO USE IT IN OOP

    If you're using object-orientated code in PHP you may want to use a call-back function that is inside an object.

    If you have a class called, for instance HTML, you have to use this code line

    ob_start(["HTML","removeWhitespace"]); 
    
    0 讨论(0)
提交回复
热议问题