PHP bufffer输出缩小,而不是textarea/pre

Beh*_*ens 2 php preg-replace minify preg-match

我正在使用缓冲区清理程序,如PHP手册评论中所示,但在textareas中遇到双重换行问题.

从我的数据库中拉出一个字符串,包含double/triple/quadruple换行符,并将其放入a中时textarea,换行符将减少到只有一个换行符.

因此:是否有可能具备的功能排除之间的所有输出<pre>,<textarea>并且</pre>,</textarea>

看到这个问题,如何在不删除IE条件注释的情况下缩小php html输出?,我想我需要使用preg_match,但我不知道如何将它实现到这个函数中.

我正在使用的功能是

function sanitize_output($buffer) {
    $search = array(
        '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
        '/[^\S ]+\</s',  // strip whitespaces before tags, except space
        '/(\s)+/s'       // shorten multiple whitespace sequences
    );

    $replace = array(
        '>',
        '<',
        '\\1'
    );

    $buffer = preg_replace($search, $replace, $buffer);

    return $buffer;
}

ob_start("sanitize_output");
Run Code Online (Sandbox Code Playgroud)

是的,我正在使用这种消毒杀菌剂,并GZIP尽可能获得最小的尺寸.

Fuz*_*yma 6

这是评论中提到的功能的实现:

function sanitize_output($buffer) {

    // Searching textarea and pre
    preg_match_all('#\<textarea.*\>.*\<\/textarea\>#Uis', $buffer, $foundTxt);
    preg_match_all('#\<pre.*\>.*\<\/pre\>#Uis', $buffer, $foundPre);

    // replacing both with <textarea>$index</textarea> / <pre>$index</pre>
    $buffer = str_replace($foundTxt[0], array_map(function($el){ return '<textarea>'.$el.'</textarea>'; }, array_keys($foundTxt[0])), $buffer);
    $buffer = str_replace($foundPre[0], array_map(function($el){ return '<pre>'.$el.'</pre>'; }, array_keys($foundPre[0])), $buffer);

    // your stuff
    $search = array(
        '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
        '/[^\S ]+\</s',  // strip whitespaces before tags, except space
        '/(\s)+/s'       // shorten multiple whitespace sequences
    );

    $replace = array(
        '>',
        '<',
        '\\1'
    );

    $buffer = preg_replace($search, $replace, $buffer);

    // Replacing back with content
    $buffer = str_replace(array_map(function($el){ return '<textarea>'.$el.'</textarea>'; }, array_keys($foundTxt[0])), $foundTxt[0], $buffer);
    $buffer = str_replace(array_map(function($el){ return '<pre>'.$el.'</pre>'; }, array_keys($foundPre[0])), $foundPre[0], $buffer);

    return $buffer;
}
Run Code Online (Sandbox Code Playgroud)

总是存在优化的空间但是有效