如何修改输出缓冲区?

How to modify output buffer?

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

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网站)。我在google上搜索过,只知道如何获取内容,clean flush。但我不知道如何修改缓冲区内容。有办法修改吗?

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

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;
}