douzhangkui2467 2012-02-02 06:53
浏览 58
已采纳

php ob_start()修复以前输出的标签

I just recently started toying with output buffering and seem to have run into a roadblock. My website utilizes a template system that loads html header/footer and the correct content template. Problem is the html headers are set in the first template loaded. So when (if) another template is loaded that contains headers (stored in a string $headers) it won't add them to the header. I have created a real sloppy way of doing this, I am looking for suggestions on to better handle this.

index.php

    ob_start('ob_html_headers');

the callback function

    function ob_html_headers($buffer)
    {
        global $headers;

        return str_replace('</head>', $headers.'</head>', $buffer);
    }

Any help is appreciated.

  • 写回答

2条回答 默认 最新

  • dppx9253 2012-02-02 07:02
    关注

    Hmmm, interesting question. If you are trying to do what I think you are, you'd probably be better off building up the data to be sent per-section, then echoing is all out the end. You can nest ob_start if you don't want to go about converting all your echos to $str .= quite yet.

    Basically what you seem to want to do is to allow later information to affect earlier output, the best way to do that is build a structure (don't worry a few arrays of strings could suffice) that represents your page, then "render" it at the end when you know where everything needs to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?