PHP加载一个. PHP页面并替换变量,然后存储输出


php load up a .php page and replace variable, and then store the output

我想加载不同的。php页面(.php包含html和一些php变量需要替换。

例如:

load.php

$output = ";

加载test.php并将$this->名称替换为value.

将HTML存储到$output


加载test1.php并将$this->名称替换为value.

附加到前面的$output变量

那么在最后我将有一个$output变量包含所有更新过的html

欢迎提出任何建议。


test.php>

<html>
<?php echo $this->name; ?>
</html>

test1.php>

<html>
<?php echo $this->address; ?>
</html>

您可能希望在require或include语句中使用输出缓冲:

ob_start();
require('load.php');
$output = ob_get_contents();
ob_end_clean();

$output应该包含load.php的内容和处理过的变量。

要处理多个文件(或其他任何文件),只需在ob_start()和最后两行之间运行它,因此您可以像这样抓取两个文件:

ob_start();
require('test.php');
require('test1.php');
$output = ob_get_contents();
ob_end_clean();