Linux中的Php换行符(ubuntu)显示^ M.(Php newline in Linux (ubuntu) show ^M)

编程入门 行业动态 更新时间:2024-10-28 18:33:49
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 ); ?>

更多推荐

本文发布于:2023-08-02 17:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:换行符   Php   Linux   show   newline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!