문제

I'd like to know if it's possible to convert the endings lines mac (CR : \r) to windows (CRLF : \r\n) with a php script.

Indeed I've got a php script which run periodically on my computer to upload some files on a FTP server and the ending lines need to be changed before the upload. It's easy to do it manually but I would like to do it automatically.

도움이 되었습니까?

해결책 3

In the end the safer way is to change what you don't want replaced first, here my function :

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

다른 팁

Can you just use a simple regular expression like the following?

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

It's probably not the most elegant or fastest solution but it should work pretty well (i.e it won't mess up existing Windows (CRLF) line-endings in a string).

Explanation

(?<=     - 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

Basically load the file to a string and call something like :

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

This is a snippet from here, last regeg might need some further tinkering with.

Edit: Fixed logic to remove duplicate replacements.

I tested it but there's some error : it seems that instead of replacing the CR ending-line it add a CRLF ending-line, here's the function, i slightly modified it to avoid to open the file outside this function :

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

it might be easier to remove all \r characters and then replace \n with \r\n.

this will take care of all variations:

$output = str_replace("\n", "\r\n", str_replace("\r", '', $input));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top