如何使 php 使用呈现的 php 代码创建 html 文件?
How to make php create an html file with rendered php code?
我刚刚开始学习 php 并且我创建了一个 php 动态创建页面的页面。例如,我对 menu.php、header.php 等使用了包含。
我想 php 创建一个 html 文件,其中包含自动呈现的 php。
我的项目:
每次我刷新索引页面时,都会加载完全不同的内容,我想让它自动创建一个 html 版本。
如果我没理解错的话,你想把PHP脚本生成的输出存储到一个文件中,对吧?在这种情况下,您可以使用输出缓冲:
<?php
// Turn on output buffering
ob_start();
// Create the HTML page content
print '<!DOCTYPE html><html><head><title>test</title></head><body><button type="button">Button</button></body></html>';
// At the end of the PHP script, write the buffered content to a file
$content = ob_get_clean();
file_put_contents('/my/html/pages/page.html', $content);
// Output the HTML to the browser
print $content;
?>
访问 php.net 了解更多信息。
我刚刚开始学习 php 并且我创建了一个 php 动态创建页面的页面。例如,我对 menu.php、header.php 等使用了包含。 我想 php 创建一个 html 文件,其中包含自动呈现的 php。
我的项目: 每次我刷新索引页面时,都会加载完全不同的内容,我想让它自动创建一个 html 版本。
如果我没理解错的话,你想把PHP脚本生成的输出存储到一个文件中,对吧?在这种情况下,您可以使用输出缓冲:
<?php
// Turn on output buffering
ob_start();
// Create the HTML page content
print '<!DOCTYPE html><html><head><title>test</title></head><body><button type="button">Button</button></body></html>';
// At the end of the PHP script, write the buffered content to a file
$content = ob_get_clean();
file_put_contents('/my/html/pages/page.html', $content);
// Output the HTML to the browser
print $content;
?>
访问 php.net 了解更多信息。