Remove all the line breaks from the html source

前端 未结 9 1980
情书的邮戳
情书的邮戳 2020-11-27 04:50

Well I know obfuscation is a bad idea. But I want all of my html code to come in one long single line. All the html tags are generated through PHP, so I think its possible.

相关标签:
9条回答
  • 2020-11-27 05:09

    This is an improved function of the above. It adds text area protection and also anything that is a tag remains untouched.

    I also removed strlen in the loop (its static).

    This might run faster as a one pass filter to check for any of the protected parts. For such a small protected_parts array it's going to be more efficient than looping through the $str four times.

    Also this doesn't fix: class = " " (the extra spaces between = and ") as its stuff inside the tags.

    function MinifyHTML($str) {
    $protected_parts = array('<pre>,</pre>','<textarea>,</textarea>', '<,>');
    $extracted_values = array();
    $i = 0;
    foreach ($protected_parts as $part) {
        $finished = false;
        $search_offset = $first_offset = 0;
        $end_offset = 1;
        $startend = explode(',', $part);
        if (count($startend) === 1) $startend[1] = $startend[0];
        $len0 = strlen($startend[0]); $len1 = strlen($startend[1]);
        while ($finished === false) {
            $first_offset = strpos($str, $startend[0], $search_offset);
    
            if ($first_offset === false) $finished = true;
            else {
                $search_offset = strpos($str, $startend[1], $first_offset + $len0);
                $extracted_values[$i] = substr($str, $first_offset + $len0, $search_offset - $first_offset - $len0);
                $str = substr($str, 0, $first_offset + $len0).'$$#'.$i.'$$'.substr($str, $search_offset);
                $search_offset += $len1 + strlen((string)$i) + 5 - strlen($extracted_values[$i]);
                ++$i;
            }
        }
    }
    $str = preg_replace("/\s/", " ", $str);
    $str = preg_replace("/\s{2,}/", " ", $str);
    $replace = array('> <'=>'><', ' >'=>'>','< '=>'<','</ '=>'</');
    $str = str_replace(array_keys($replace), array_values($replace), $str);
    
    for ($d = 0; $d < $i; ++$d)
        $str = str_replace('$$#'.$d.'$$', $extracted_values[$d], $str);
    
    return $str;
    }
    
    0 讨论(0)
  • 2020-11-27 05:16
    $output = preg_replace('!\s+!m', ' ', $output);
    
    0 讨论(0)
  • 2020-11-27 05:17

    You can do :

    $output = '<p>'.
                  '<div class="title">Hello</div>'.
               '</p>';
    

    This way, $output won't contain any line jump.

    This should also work :

    $output = preg_replace(array('/\r/', '/\n/'), '', $output);
    
    0 讨论(0)
  • 2020-11-27 05:18

    Maybe this?

    $output = str_replace(array("\r\n", "\r"), "\n", $output);
    $lines = explode("\n", $output);
    $new_lines = array();
    
    foreach ($lines as $i => $line) {
        if(!empty($line))
            $new_lines[] = trim($line);
    }
    echo implode($new_lines);
    
    0 讨论(0)
  • 2020-11-27 05:21

    You can try this perhaps.

    // Before any output
    ob_start();
    
    // End of file
    $output = ob_get_clean();
    echo preg_replace('/^\s+|\n|\r|\s+$/m', '', $output);
    

    This should, unless I messed up the regex, catch all output, and then replace all new line characters as well as all whitespace at the end and beginning of lines.

    If you already have all output collected in a variable, you can of course just use the last line directly and skip the output buffering stuff :)

    0 讨论(0)
  • 2020-11-27 05:23

    This is already well answered, but you may be able to do more than just trim spaces at both ends of each line:

    1. First extract all text within quotes (you don't want to touch those), replace with a marker with a sequence number, store the sequence number with the text
    2. Extract all text within <script></script> tags and do the same as step #1
    3. Replace all white-space (including \n, \r) with spaces
    4. Replace all >1 space sequences with 1 space
    5. Replace all >_< with >< (_ = space)
    6. Replace all _>, <_ and </_ with >, < and </ (_ = space)
    7. Replace markers with the actual texts

    This procedure can potentially compact the entire HTML file. This takes advantage of the fact that multiple white-space text inside HTML tags are intepreted as one single space.

    0 讨论(0)
提交回复
热议问题