doubing3662 2012-11-26 19:07
浏览 22
已采纳

如何将某些内容添加到PHP输出缓冲区的开头?

How do you append something to the beginning of the output buffer?

For example, say you have the following code:

ob_start();

echo '<p>Start of page.</p>';
echo '<p>Middle of page.</p>';
echo '<p>End of page</p>';

Before flushing the contents to the browser, how can I append something so that it appears before <p>Start of page.</p> when the page loads?

It sounds simple enough, like moving the pointer to the beginning of an array, but I couldn't find how to do it with the output buffer.

  • 写回答

5条回答 默认 最新

  • dongzouxigu12345 2012-11-26 19:14
    关注

    ** PHP 5.3 **

    ob_start(function($output) {
        $output = '<p>Prepended</p>'.$output;
        return $output;
    });
    
    echo '<p>Start of page.</p>';
    echo '<p>Middle of page.</p>';
    echo '<p>End of page</p>';
    

    ** PHP < 5.3 **

    function prependOutput($output) {
        $output = '<p>Appended</p>'.$output;
        return $output;
    }
    
    ob_start('prependOutput');
    
    echo '<p>Start of page.</p>';
    echo '<p>Middle of page.</p>';
    echo '<p>End of page</p>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB图像问题
  • ¥20 树莓派5做人脸情感识别与反馈系统
  • ¥15 selenium 控制 chrome-for-testing 在 Linux 环境下报错 SessionNotCreatedException
  • ¥15 使用pyodbc操作SQL数据库
  • ¥15 MATLAB实现下列
  • ¥30 mininet可视化打不开.mn文件
  • ¥50 C# 全屏打开Edge浏览器
  • ¥80 WEBPACK性能优化
  • ¥30 python拟合回归分析
  • ¥500 metaswitch 6010