I have a file that I have to read in and modify in PHP, and so I read it in, explode it by new lines, modify what I need, then re-stick it together re-adding newlines via \n.

However, it seems to be outputting them as \r\\n for some reason. The file before any modification has new lines as \r\n which seems to work fine. Only when I add the new lines via \n does it break into \r\\n.

Also after some reading, I tried the newlines as \r\n instead of just \n, but that outputs as \r\\r\\n. Any ideas guys? Thanks a lot! If you need to see anything else or require more info, just ask.

The code that add's the new lines is here:

for($i = 0 ; $i<count($tmp);$i++){
        $tmpstr .= $tmp[$i];
        if(count($tmp)-1 != $i){
            $tmpstr .= '\r\n';
        }
    }

It adds newlines at the end of all but the last line, that seems to be working fine except for the wrong characters.

有帮助吗?

解决方案

Change $tmpstr .= '\r\n';
To: $tmpstr .= "\r\n" # Use double quotes;

其他提示

you have to use double quotes "\r\n"

You can also use the predefined constant PHP_EOL (End Of Line):

$tmpstr .= PHP_EOL;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top