【问题标题】:How to modify output buffer?如何修改输出缓冲区?
【发布时间】:2016-11-24 10:56:29
【问题描述】:

我正在尝试修改输出缓冲区的内容。 我得到了这样的东西:

if($this->page == 'login')
{
                $output = ob_get_contents();    // $output is index.html            
                //  have no idea how to modify `$output`
}

还有我的index.html

<html>
  <head></head>
  </body>
     if($usr ==null) include 'login.php';
      else include main.php; //`main.php`  with header bar and footer bar
    // this's where i want to clean and insert my login form (login.php)
  </body>
</html>

login.php:

<?php
  <form>
      .....
  </form>
?>

我有$output 但我想清理所有并将login.php 的来源插入正文中,以便每个人都可以看到我的登录表单(网站样式有css)。我在谷歌上搜索过,只知道如何获取内容、清理和刷新。但我看不到如何修改缓冲区内容。有办法修改吗?

【问题讨论】:

    标签: php html css output php-5.5


    【解决方案1】:

    我不确定您的其余代码是如何工作的。但也许你可以使用标签来替换。像这样的。

    index.html
    <html>
      <head></head>
      </body>
        {{body}}
      </body>
    </html>
    
    
    <?php
    //Logic file
    if($this->page == 'login')
    {
        $output = ob_get_contents();    // $output is index.html 
        ob_clean();
        ob_start();
        include 'login.php';
        $login_content = ob_get_contents();    // $output is index.html  
        ob_clean();
        $content = str_replace('{{body}}', $login_content, $output);
    
        echo $content;
    }
    

    【讨论】:

    • 实际上,我是在 index.php 中完成的,但我认为它的代码太差了。所以我尝试了 ob_get_content 以希望修改输出。
    • 应该可以。您的 index.html 是否被用作所有页面的模板?我已将其更新为 {{body}}。您可以使用它来将其替换为其他页面中的任何内容。
    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多