如何做 PHP 读取指定的 PHP 文件解析后输出?

这个指定的PHP文件如下:
demo.php

<?php $title = 'This is title for demo';?>
<html>
<head><title><?php echo $title; ?></title></head>
<body>
<?php
$member = ['father', 'mother', 'son'];
foreach ($member as $v) {
    echo $v . "<br />";
}
?>
</body>
</html>

现在需要编写一个php文件,读取demo.php并将其解析成html后输出,该怎么写?下面是写的伪代码,希望小伙伴给点儿建议,能够帮助实现就更好了。

$string = file_get_contents('demo.php');
ob_start();
$string;
$html = ob_get_contents();
ob_end_clean();
echo $html;
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

我暂时想到的就是这样。

$string = file_get_contents('demo.php');
ob_start();
eval('?>' . $string);
$html = ob_get_contents();
ob_end_clean();
echo $html;

用到了eval这个函数

6年前 评论
讨论数量: 7
leo

require 'demo.php';

6年前 评论

@leo 不能用require需要file_get_contents读取之后再解析这个php文件然后输出成html

6年前 评论
leo

@江边望海 为何不能 require

6年前 评论

@leo 因为,有可能这个demo.php不是以文件存储的,有可能这个demo.php的代码是在数据库中存储的。

6年前 评论

我暂时想到的就是这样。

$string = file_get_contents('demo.php');
ob_start();
eval('?>' . $string);
$html = ob_get_contents();
ob_end_clean();
echo $html;

用到了eval这个函数

6年前 评论
leo

@江边望海 stream_wrapper_register 了解一下,发挥你的想象

6年前 评论

@leo 好的,谢谢。我研究一下。

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!