文件1:
<?php
<div>
<p>
something...
</p>
</div>
?>
Run Code Online (Sandbox Code Playgroud)
文件2:
<?php
$file1= What should I write to get output of File1?
?>
Run Code Online (Sandbox Code Playgroud)
我需要获取$ file1变量的html输出; 请帮忙.
我认为这对你有用:
function read($file) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
$file = 'path/to/your/file.php';
$content = read($file);
Run Code Online (Sandbox Code Playgroud)
要传递变量,您可以修改上面的函数,如下所示:
function read($file, $vars) {
ob_start();
extract($vars, EXTR_SKIP);
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
$file = 'path/to/your/file.php';
$vars = array(
'var1' => 'value',
);
$content = read($file, $vars);
Run Code Online (Sandbox Code Playgroud)
您可以像这样访问包含文件中的变量:print $ var1;
归档时间: |
|
查看次数: |
4511 次 |
最近记录: |