You can also do...
$fixed=str_replace("\n",'\n',$mystring);
...before writting and, when reading....
$mystring=str_replace('\n',"\n",$fromfile);
.. after read the string from file.
So yo will get the original content of the string.
If the text is edited in a "TextArea" html control, you must
replace also the CR characters as follows...
$fixed=str_replace("\n",'\n',str_replace("\r",'\r',$mystring));
..or in a more more compact style...
$fixed=addcslashes($mystring,"\r\n");
and to rebuild the original string...
$mystring=str_replace('\n',"\n",str_replace('\r',"\r",$fixed));