将 php 脚本输出打印到文件 [已关闭]

php
2022-08-30 19:43:00

PHP中是否有本机函数或一组函数,允许我将php文件输出打印到文件中。echo

例如,代码将生成需要放入.html文件中的HTML DOM,然后显示为静态页面。


答案 1

最简单的方法是创建一个 HTML 数据字符串并使用 file_put_contents() 函数。

$htmlStr = '<div>Foobar</div>';
file_put_contents($fileName, $htmlStr);

若要创建此字符串,需要捕获所有输出的数据。为此,您需要使用 和 输出控制函数:ob_startob_end_clean

// Turn on output buffering
ob_start();
echo "<div>";
echo "Foobar";
echo "</div>";

//  Return the contents of the output buffer
$htmlStr = ob_get_contents();
// Clean (erase) the output buffer and turn off output buffering
ob_end_clean(); 
// Write final string to file
file_put_contents($fileName, $htmlStr);

参考资料 -


答案 2
file_put_contents($filename, $data)

http://php.net/manual/en/function.file-put-contents.php


推荐