我在 Ubuntu 服务器上运行 PHP 脚本。我正在用 PHP 读取配置文件,做一些 preg_replace
s 并写回该文件。使用 file_get_contents
进行操作和 file_put_contents
.
但是当我这样做时,我会看到 ^M
在每一行的末尾。
为什么它在那里?如何在不出现这些字符的情况下写入文件?还是忽略它们可以节省?
最佳答案
这是我的小脚本,将 Windows 行分隔符替换为 UNIX 样式。
<?php
$in_fp = fopen( "input.txt", "r" ) or die;
$out_fp = fopen( "output.txt", "w" ) or die;
while ( !feof( $in_fp ) ) {
$buf = fread( $in_fp, 5000 );
$buf = str_replace( "\r\n", "\n", $buf );
fwrite( $out_fp, $buf );
}
fclose( $out_fp );
fclose( $in_fp );
?>
关于Linux (ubuntu) 中的 PHP 换行符显示 ^M,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662495/