Linux中的Php换行符(ubuntu)显示^ M.(Php newline in Linux (ubuntu) show ^M)
我在Ubuntu服务器上运行PHP脚本。 我正在使用PHP读取配置文件,执行一些preg_replace并写回该文件。 使用file_get_contents和file_put_contents执行此操作。
但是当我这样做时,我会在每一行的末尾看到^M
它为什么存在,如何在不显示这些字符的情况下写入文件? 或者是否可以忽略它们?
I'm running a PHP script on a Ubuntu server. I'm reading a config file with PHP, do some preg_replaces and write back to that file. Doing that with file_get_contents and file_put_contents.
But when I do that I see ^M at the end of each line.
Why is it there and how can I write to a file withou hacing those characters appear? Or is it save to ignore them?
最满意答案
这是我的小脚本,它将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 ); ?>Here is my little script, who replace Windows line separators to UNIX style.
<?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 ); ?>更多推荐
发布评论