php file_put_contents() 写入回车

PHP file_put_contents() 函数是一次性向文件写入字符串或追加字符串内容的最合适选择。

file_put_contents()

file_put_contents() 函数用于把字符串写入文件,成功返回写入到文件内数据的字节数,失败则返回 FALSE。

file_put_contents() 的行为实际上等于依次调用 fopen()fwrite() 以及 fclose() 功能一样。

在php中,用file_put_contents() 把字符串写入文件时,有时候发现写入的内容不能换行。

如:

<?php
$a = '<?php 
 $a["1"]=a; 
 $a["2"]=b; 
 return $a; 
';
file_put_contents('test.php', $a);
?>

test.php 的结果是:

<?php $a["1"]=a; $a["2"]=b; return $a;

内容中没有回车。

在网上查找解决方法,说是要把输入的内容用""来代替''。试验了下,结果可以。

<?php
$a = "<?php 
 $a['1']=a; 
 $a['2']=b; 
 return $a; 
";
file_put_contents('test.php', $a);

?>

test.php 的结果是:

<?php 
 $a['1']=a; 
 $a['2']=b; 
 return $a; 
原文地址:https://www.cnblogs.com/zjfazc/p/3314517.html