获取 PHP 中字符串中包含的结果?
假设文件测试.php如下所示:
<?php
echo 'Hello world.';
?>
我想做这样的事情:
$test = include('test.php');
echo $test;
// Hello world.
任何人都可以指出我正确的道路吗?
编辑:
我最初的目标是从数据库中提取与HTML混合的PHP代码并进行处理。以下是我最终所做的:
// 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);
}