Linux (ubuntu) 中的 PHP 换行符显示 ^M

标签 php ubuntu file-get-contents

我在 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/

相关文章:

ubuntu - 在 Ubuntu 18 上安装 Xcas 时出错 - mpfr 版本错误

python - 使用 PyGTK 的滑动布局

php - javascript 到 php 中不会触发,直到 file_get_contents 返回数据

json - 如何使用Powershell修改Json

php - 当查询有 "distinct"时,Zend Paginator 显示错误的页数

PHP 文件既没有 move 也没有未 move

debugging - Ubuntu下IntelliJ IDEA go-lang-idea-plugin Alpha调试

javascript - 我们如何使用 php mysql 给已经存在的电子邮件 ID?

php - 加密是否可以防止 sql 注入(inject)?

PHP file_get_contents/CURL 不返回整页