Hi, I'm trying to write a string passed from a <textarea> through an ajax function to a txt file. I know that a newline is written in the textarea using \n, but in the text file using \r\n (I'm on Windows), so I used preg_replace() to replace any instance of \n with \r\n. When I send the string "I redid this file.\nIt now has two lines." to php, the text file shows:
"I redid this file.\r\nIt now has two lines. ".
When I commented out the preg_replace(), the text file showed:
"I redid this file.It now has two lines."
Here is the code I'm using. (I've verified that the newline character is being sent by checking the string sent using firebug)
Any ideas?PHP Code:$file = urldecode($_GET['file']);
$text = urldecode($_GET['text']);
$text = preg_replace("/\\n/", "\\r\\n", $text);
if(!is_file($file)){
die('There was a problem saving the file. It may no longer exist.');
}
$handle = fopen($file, 'w');
if(fwrite($handle, $text)){
print 'The file was successfully saved.';
print $text;
}
else{
die('There was a problem saving the file. It may be write-protected.');
}


Reply With Quote
Bookmarks