如何修改輸出緩衝區? (How to modify output buffer?)


問題描述

如何修改輸出緩衝區? (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 作為網站的樣式)。我在谷歌上搜索過,只知道如何獲取內容、清理和刷新。但我看不到如何修改緩衝區內容。有辦法修改嗎?


參考解法

方法 1:

I'm not sure of how the rest of your code works. But maybe you could use a tag to replace with. Something like this.

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

(by Haruji BurkeAntony Thompson)

參考文件

  1. How to modify output buffer? (CC BY‑SA 2.5/3.0/4.0)

#php-5.5 #css #output #PHP #html






相關問題

當作為參數傳遞時,PHP 如何解釋和評估函數和匿名函數? (How does PHP interpret and evaluate function and anonymous function when passed as an argument?)

使用 symfony 時 PHP 5.5 無法識別服務器 (PHP 5.5 won't recognise server when using symfony)

升級到 PHP5.5 時 Wordpress 崩潰 (Wordpress crashes when upgrading to PHP5.5)

從數據庫返回多個值 (return multiple values from database)

PHP:嵌套 foreach 循環的問題 (PHP: Issue with nested foreach loops)

如果條件在幾小時、幾天內不起作用 (if condition not working for Hours,Days)

Slim 3.3 輸出中缺少字符 (Slim 3.3 missing characters in output)

PHP排序對象值但保留原始相對順序 (PHP sort objects value but retain original relative order)

遇到錯誤無法加載請求的文件:helpers/form_validation_helper.php (An Error Was Encountered Unable to load the requested file: helpers/form_validation_helper.php)

如何從類的函數內部訪問全局變量 (How to access global variable from inside class's function)

如何修改輸出緩衝區? (How to modify output buffer?)

在同一台 Ubuntu 服務器上安裝和配置 PHP5 和 7 (Install and configure PHP5 and 7 on same Ubuntu server)







留言討論