我有以下场景.
我有一个PHP网站,包含大约3个带有动态内容的网页.我想将此呈现为静态内容到另一个文件.例如.contact.php - > contact.html
我的代码看起来像这样
ob_start();
$content = require_once 'contact.php';
ob_end_flush();
file_put_contents("contact.html", $content);
Run Code Online (Sandbox Code Playgroud)
不知怎的,这不起作用; - ?(
require_once()不返回脚本输出的内容.您需要获取存储在输出缓冲区中的脚本输出:
ob_start();
require_once('contact.php');
$content = ob_get_clean();
file_put_contents('contact.html', $content);
Run Code Online (Sandbox Code Playgroud)
ob_get_clean():
获取当前缓冲区内容并删除当前输出缓冲区.
ob_get_clean()基本上执行ob_get_contents()和ob_end_clean().