Remove all the line breaks from the html source

前端 未结 9 2027
情书的邮戳
情书的邮戳 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条回答
  •  萌比男神i
    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 :)

提交回复
热议问题