問題描述
如何修改輸出緩衝區? (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 Burke、Antony Thompson)