打印php脚本输出到文件

Chr*_*mut 16 php

PHP中是否有本机函数或函数集允许我将echophp文件输出打印到文件中.

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

Lix*_*Lix 36

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

$htmlStr = '<div>Foobar</div>';
file_put_contents($fileName, $htmlStr);
Run Code Online (Sandbox Code Playgroud)

要创建此字符串,您需要捕获所有输出的数据.为此,您需要使用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);
Run Code Online (Sandbox Code Playgroud)

参考 -