Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

$tmpstr .= PHP_EOL;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top