file_get_contents 之前的预处理文件
Pre process file before file_get_contents
我想知道,在 PHP 中,是否有办法让 PHP 在将文件写入字符串之前对其进行预处理。
示例:
我有 index.php,它实例化一个 class,它根据通过 .htaccess 传入的 GET 参数调用 file_get_contents
。
我在 index.php 中有一些变量,例如 $error = 'This is an error';
在我用 file_get_contents
带回的一个文件中,我有:
<?php if ($error): echo $error; endif; ?>
由于 file_get_contents
字面上只是将所有内容写入字符串,因此当然不会得到处理。
有人知道如何在带回文件之前处理 PHP,或者带回 PHP 的文件吗?
请不要告诉我使用像include
这样的函数,因为我需要将文件写入字符串才能执行str_replace
在某些词上。
谢谢。
不知道为什么这个问题被疯狂地否决了......但经过大量搜索后我解决了这个问题。我需要的代码是:
ob_start();
include $this->templateDir . $this->theme . '/' . $page . '.php';
$pageContent = ob_get_contents();
ob_end_clean();
感谢投反对票并尝试解决我的问题。非常感谢。
Inb4 对此投反对票 post。
我想知道,在 PHP 中,是否有办法让 PHP 在将文件写入字符串之前对其进行预处理。
示例:
我有 index.php,它实例化一个 class,它根据通过 .htaccess 传入的 GET 参数调用 file_get_contents
。
我在 index.php 中有一些变量,例如 $error = 'This is an error';
在我用 file_get_contents
带回的一个文件中,我有:
<?php if ($error): echo $error; endif; ?>
由于 file_get_contents
字面上只是将所有内容写入字符串,因此当然不会得到处理。
有人知道如何在带回文件之前处理 PHP,或者带回 PHP 的文件吗?
请不要告诉我使用像include
这样的函数,因为我需要将文件写入字符串才能执行str_replace
在某些词上。
谢谢。
不知道为什么这个问题被疯狂地否决了......但经过大量搜索后我解决了这个问题。我需要的代码是:
ob_start();
include $this->templateDir . $this->theme . '/' . $page . '.php';
$pageContent = ob_get_contents();
ob_end_clean();
感谢投反对票并尝试解决我的问题。非常感谢。
Inb4 对此投反对票 post。