使用php脚本转换文件的结束行

时间:2013-08-22 09:13:22

标签: php line-endings

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

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

5 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分: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的片段,最后一个regeg可能需要进一步修补。

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

答案 2 :(得分:1)

最后,更安全的方法是先改变你不想要的东西,这里是我的功能:

/**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;
 }

答案 3 :(得分:0)

我测试了它但是有一些错误:似乎不是替换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;
    }

答案 4 :(得分:0)

删除所有\ r \ n字符然后将\ n替换为\ r \ n。

可能更容易

这将照顾所有变化:

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