2016-11-24 135 views
1

我试图修改输出缓冲区的内容。 我得到的东西喜欢这样的:如何修改输出缓冲区?

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作风格的网站)。我搜索了谷歌,只知道如何获得内容,干净和齐平。但我看不出如何修改缓冲区内容。有没有办法修改它?

回答

2

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

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

其实,我做了它在我的index.php,但我认为这是太糟糕的代码。所以我试图ob_get_content希望修改输出。 – Hanata

+0

这应该工作。您的index.html是否被用作所有网页的模板?我已将它更新为{{body}}。您可以使用它来替换来自其他页面的任何内容。 –

+0

哦,真好!你给我一个好主意。非常感谢。 – Hanata