删除输出部分

时间:2011-12-02 10:37:22

标签: php

我希望能够删除一些输出缓冲区部分。所以考虑这个代码:

function over_all($string) {
    if (OVER_ALL_ENABLED) { 

        # Stop output buffer
        # Clean output buffer

        echo $string;

        # Restart output buffer
    }
}

ob_start();

echo 'This is phrase 1<br>';
over_all('I don\'t want to see Phrase 1<br>');
echo 'This is phrase 2';
over_all('I don\'t want to see Phrase 2');

如果我将OVER_ALL_ENABLED设置为true,则会打印:

  

我不想看短语1   我不想看短语2

否则:

  

这是短语1
  这是短语2

你能用正确的PHP函数替换注释吗?

4 个答案:

答案 0 :(得分:3)

杰夫,我认为这段代码必须正常工作

function over_all($string) {
    if (OVER_ALL_ENABLED) { 
        ob_end_clean();
        echo $string;
        ob_start();
    }
}

但如果你在网络服务器下运行它,并在其中启用gz,它将在那里缓冲

答案 1 :(得分:2)

答案 2 :(得分:0)

使用Ob_Clean()删除输出缓冲区中的任何内容。

您的功能应如下所示:

function over_all($string) {
    if (OVER_ALL_ENABLED) {
        Ob_Clean ();
        echo $string;
    }
}

答案 3 :(得分:0)

我想,它应该是这样的:

$new_output = "";

function over_all($string) {
    if (OVER_ALL_ENABLED) {
        $new_output .= $string;
        Ob_Clean ();
    }
};

ob_start();

echo 'This is phrase 1<br>';
over_all('I don\'t want to see Phrase 1<br>');
echo 'This is phrase 2';
over_all('I don\'t want to see Phrase 2');

echo $new_output;

ob_end_flush();

$new_output变量就像你有第二个缓冲区(一个未被ob_clean清除的缓冲区)。