假设文件test.php看起来像这样:
<?php
echo 'Hello world.';
?>
我想做这样的事情:
$test = include('test.php');
echo $test;
// Hello world.
有人能指出我正确的道路吗?
编辑:
我最初的目标是将PHP代码与HTML混合在一起并将其处理出来.这是我最终做的事情:
// Go through all of the code, execute it, and incorporate the results into the content
while(preg_match('/<\?php(.*?)\?>/ims', $content->content, $phpCodeMatches) != 0) {
    // Start an output buffer and capture the results of the PHP code
    ob_start();
    eval($phpCodeMatches[1]);
    $output = ob_get_clean();
    // Incorporate the results into the content
    $content->content = str_replace($phpCodeMatches[0], $output, $content->content);
}
Sla*_*rix 57
使用输出缓冲是最好的选择.
ob_start();
include 'test.php';
$output = ob_get_clean();
PS:请记住,如果需要,您也可以将输出缓冲区嵌套到心中.
test.php的
<?php
return 'Hello World';
?>
<?php
$t = include('test.php');
echo $t;
?>
只要包含的文件有一个return语句就可以了.
| 归档时间: | 
 | 
| 查看次数: | 9031 次 | 
| 最近记录: |