PHP缓冲区输出缩小,而不是textarea / pre

时间:2015-01-10 16:00:54

标签: php preg-replace preg-match minify

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

从我的数据库中拉出一个字符串,包含双/三/换四行换行符,并将其放入textarea时,换行符将缩减为仅一个换行符。

因此:是否可以让该功能排除<pre><textarea></pre></textarea>之间的所有输出?

看到这个问题How to minify php html output without removing IE conditional comments?,我想我需要使用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");

是的,我同时使用这种消毒剂和GZIP来获得最小尺寸。

5 个答案:

答案 0 :(得分: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;
}

总是存在优化的空间,但是可行的

答案 1 :(得分:0)

PRE有一个简单的解决方案不适用于TEXTAREA:将空格替换为&nbsp;,然后使用nl2br()将换行符替换为BR输出值之前的元素。它不优雅,但有效:

<pre><?php
    echo(nl2br(str_replace(' ', '&nbsp;', htmlspecialchars($value))));
?></pre>

不幸的是,它不能用于TEXTAREA,因为浏览器会将<br />显示为文字。

答案 2 :(得分:0)

答案 3 :(得分:0)

也许这会为您提供所需的结果。 但总的来说,我不推荐这种消毒工作,它对性能不利。在这些日子里,并不需要从html输出中删除空白字符。

function sanitize_output($buffer) {
    $ignoreTags = array("textarea", "pre");

    # find tags that must be ignored and replace it with a placeholder
    $tmpReplacements = array();
    foreach($ignoreTags as $tag){
        preg_match_all("~<$tag.*?>.*?</$tag>~is", $buffer, $match);
        if($match && $match[0]){
            foreach($match[0] as $key => $value){
                if(!isset($tmpReplacements[$tag])) $tmpReplacements[$tag] = array();
                $index = count($tmpReplacements[$tag]);
                $replacementValue = "<tmp-replacement>$index</tmp-relacement>";
                $tmpReplacements[$tag][$index] = array($value, $replacementValue);
                $buffer = str_replace($value, $replacementValue, $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);

    # re-insert previously ignored tags
    foreach($tmpReplacements as $tag => $rows){
        foreach($rows as $values){
            $buffer = str_replace($values[1], $values[0], $buffer);
        }
    }

    return $buffer;
}

答案 4 :(得分:0)

这是我清理过的HTML版本。我已经注释了代码,因此应该清楚它在做什么。

function comprimeer($html = '', $arr_tags = ['textarea', 'pre']) {
    $arr_found = [];
    $arr_back = [];
    $arr_temp = [];

    // foreach tag get an array with tag and its content
    // the array is like: $arr_temp[0] = [ 0 = ['<tag>content</tag>'] ];
    foreach ($arr_tags as $tag) {
        if(preg_match_all('#\<' . $tag . '.*\>.*\<\/' . $tag . '\>#Uis', $html, $arr_temp)) {
            // the tag is present
            foreach($arr_temp as $key => $arr_item) {
                // for every item of the tag keep the item
                $arr_found[$tag][] = $arr_item[0];
                // make an nmubered replace <tag>1</tag>
                $arr_back[$tag][] = '<' . $tag . '>' . $key . '</' . $tag . '>';
            }
            // replace all the present tags with the numbered ones
            $html = str_replace((array) $arr_found[$tag], (array) $arr_back[$tag], $html);
        }
    } // end foreach

    // clean the html
    $arr_search = [
        '/\>[^\S ]+/s', // strip whitespaces after tags, except space
        '/[^\S ]+\</s', // strip whitespaces before tags, except space
        '/(\s)+/s'     // shorten multiple whitespace sequences
    ];
    $arr_replace = [
        '>',
        '<',
        '\\1'
    ];
    $clean = preg_replace($arr_search, $arr_replace, $html);

    // put the kept items back
    foreach ($arr_tags as $tag) {
        if(isset($arr_found[$tag])) {
            // the tag was present replace them back
            $clean = str_replace($arr_back[$tag], $arr_found[$tag], $clean);
        }   
    } // end foreach
    // give the cleaned html back
    return $clean;
} // end function