Lix*_*Lix 36
最简单的方法是创建HTML数据字符串并使用该file_put_contents()函数.
$htmlStr = '<div>Foobar</div>';
file_put_contents($fileName, $htmlStr);
Run Code Online (Sandbox Code Playgroud)
要创建此字符串,您需要捕获所有输出的数据.为此,您需要使用ob_start和ob_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)
参考 -