jef*_*n24 4 php include server-side-includes
我试图保持我的代码干净将其中的一些分解成文件(有点像库).但其中一些文件需要运行PHP.
所以我想做的是:
$include = include("file/path/include.php");
$array[] = array(key => $include);
include("template.php");
Run Code Online (Sandbox Code Playgroud)
比在template.php我会:
foreach($array as $a){
echo $a['key'];
}
Run Code Online (Sandbox Code Playgroud)
所以我想存储php在变量运行之后发生的事情以便稍后传递.
使用file_get_contents不会运行它将它存储为字符串的php,所以有没有任何选项,或者我运气不好?
更新:
所以喜欢:
function CreateOutput($filename) {
if(is_file($filename)){
file_get_contents($filename);
}
return $output;
}
Run Code Online (Sandbox Code Playgroud)
或者你的意思是为每个文件创建一个函数?
Pas*_*TIN 10
看来你需要使用Output Buffering Control- 特别是看ob_start()和ob_get_clean()函数.
使用输出缓冲将允许您将标准输出重定向到内存,而不是将其发送到浏览器.
这是一个简单的例子:
// Activate output buffering => all that's echoed after goes to memory
ob_start();
// do some echoing -- that will go to the buffer
echo "hello %MARKER% !!!";
// get what was echoed to memory, and disables output buffering
$str = ob_get_clean();
// $str now contains what whas previously echoed
// you can work on $str
$new_str = str_replace('%MARKER%', 'World', $str);
// echo to the standard output (browser)
echo $new_str;
Run Code Online (Sandbox Code Playgroud)
你得到的输出是:
hello World !!!
Run Code Online (Sandbox Code Playgroud)