【问题标题】:convert ending line of a file with a php script使用 php 脚本转换文件的结尾行
【发布时间】:2013-08-22 09:13:22
【问题描述】:

我想知道是否可以使用 php 脚本将结尾行 mac (CR : \r) 转换为 windows (CRLF : \r\n)。

确实,我有一个 php 脚本,它会在我的计算机上定期运行以将一些文件上传到 FTP 服务器上,并且在上传之前需要更改结尾行。手动完成很容易,但我想自动完成。

【问题讨论】:

  • str_replace(array("\r\n", "\n"), array("\n", "\r\n"))?

标签: php line-endings


【解决方案1】:

你可以只使用如下的简单正则表达式吗?

function normalize_line_endings($string) {
 return preg_replace("/(?<=[^\r]|^)\n/", "\r\n", $string);
}

它可能不是最优雅或最快的解决方案,但它应该工作得很好(即它不会弄乱字符串中现有的 Windows (CRLF) 行尾)。

说明

(?<=     - Start of a lookaround (behind)
  [^\r]  - Match any character that is not a Carriage Return (\r)
  |      - OR
  ^      - Match the beginning of the string (in order to capture newlines at the start of a string
)        - End of the lookaround
\n       - Match a literal LineFeed (\n) character

【讨论】:

    【解决方案2】:

    基本上将文件加载到一个字符串并调用类似的东西:

    function normalize($s) {
      // Normalize line endings
      // Convert all line-endings to UNIX format
      $s = str_replace(array("\r", "\n"), "\r\n", $s);
      // Don't allow out-of-control blank lines
      $s = preg_replace("/\r\n{2,}/", "\r\n\r\n", $s);
      return $s;
    }
    

    这是来自here 的 sn-p,最后一个 regeg 可能需要进一步修改。

    编辑:修复了删除重复替换的逻辑。

    【讨论】:

    • 我认为逻辑有点缺陷;如果将所有\n 替换为\r\n,则将所有\r 替换为\r\n,示例字符串Hello\n 将变为Hello\r\n\n。然后,您可以绕过这个一开始就不需要的正则表达式。
    • @JasonLarke:我已经为此添加了修复程序,现在应该可以使用了。请注意这是一个概念指南,而不是最终产品:P
    【解决方案3】:

    最后更安全的方法是先更改您不想替换的内容,这是我的功能:

    /**Convert the ending-lines CR et LF in CRLF.
     * 
     * @param string $filename Name of the file
     * @return boolean "true" if the conversion proceed without error and else "false".
     */
    function normalize ($filename) {
    
        echo "Convert the ending-lines of $filename into CRLF ending-lines...";
    
        //Load the content of the file into a string
        $file_contents = @file_get_contents($filename);
    
        if (!file_contents) {
            echo "Could not convert the ending-lines : impossible to load the file.PHP_EOL";
            return false;
        }
    
        //Replace all the CRLF ending-lines by something uncommon 
        $DontReplaceThisString = "\r\n";
        $specialString = "!£#!Dont_wanna_replace_that!#£!";
        $string = str_replace($DontReplaceThisString, $specialString, $file_contents);
    
        //Convert the CR ending-lines into CRLF ones
        file_contents = str_replace("\r", "\r\n", $file_contents);
    
        //Replace all the CRLF ending-lines by something uncommon
        $file_contents = str_replace($DontReplaceThisString, $specialString, $file_contents); 
    
        //Convert the LF ending-lines into CRLF ones
        $file_contents = str_replace("\n", "\r\n", $file_contents);
    
        //Restore the CRLF ending-lines
        $file_contents = str_replace($specialString, $DontReplaceThisString, $file_contents);
    
        //Update the file contents
        file_put_contents($filename, $file_contents);
    
        echo "Ending-lines of the file converted.PHP_EOL";
        return true;
     }
    

    【讨论】:

      【解决方案4】:

      我对其进行了测试,但出现了一些错误:似乎不是替换 CR 结束行而是添加了一个 CRLF 结束行,这是函数,我对其进行了一些修改以避免在此函数之外打开文件:

      // FONCTION CONVERTISSANT LES FINS DE LIGNES CR TO CRLF
          function normalize ($filename) {
      
              echo "Convert the ending-lines of $filename... ";
      
              //Load the file into a string
              $string = @file_get_contents($filename);
      
              if (!string) {
                  echo "Could not convert the ending-lines : impossible to load the file.\n";
                  return false;
              }
      
              //Convert all line-endings
              $string = str_replace(array("\r", "\n"), "\r\n", $string);
              // Don't allow out-of-control blank lines
              $string = preg_replace("/\r\n{2,}/", "\r\n", $string);
      
              file_put_contents($filename, $string);
      
              echo "Ending-lines converted.\n";
              return true;
          }
      

      【讨论】:

        【解决方案5】:

        删除所有 \r 字符然后将 \n 替换为 \r\n 可能会更容易。

        这将处理所有变化:

        $output = str_replace("\n", "\r\n", str_replace("\r", '', $input));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-05-15
          • 1970-01-01
          • 2011-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-25
          相关资源
          最近更新 更多