use*_*027 1 php printing arrays file
我有:
echo"<br>";echo"<br><pre>";print_r($array2);echo"</pre>";
echo"<br>";echo"<br><pre>";print_r($array3);echo"</pre>";
echo"<br>";echo"<br><pre>";print_r($array4);echo"</pre>";
Run Code Online (Sandbox Code Playgroud)
我需要将所有这些print_r打印保存到文件中(不会在页面中出现任何内容).
我知道我需要做那样的事情:
$f = fopen("file.txt", "w");
fwrite($f, "TEXT TO WRITE");
fclose($f);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将内容放在其中.
Thansk一百万
Dav*_*nde 14
您可以使用输出缓冲,当您想要控制PHP脚本中输出的内容以及如何输出它时,它非常方便.这是一个小样本:
ob_start();
echo"<br>";echo"<br><pre>";print_r($array2);echo"</pre>";
echo"<br>";echo"<br><pre>";print_r($array3);echo"</pre>";
echo"<br>";echo"<br><pre>";print_r($array4);echo"</pre>";
$content = ob_get_contents();
$f = fopen("file.txt", "w");
fwrite($f, $content);
fclose($f);
Run Code Online (Sandbox Code Playgroud)
编辑:如果你不想在页面中显示输出,你只需要调用ob_end_clean():
ob_start();
//...
$content = ob_get_contents();
ob_end_clean();
//... write the file, either with fopen or with file_put_contents
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16693 次 |
| 最近记录: |