I am trying to save the content of a textarea to a text file, but am having some trouble with extra line breaks being inserted.
As an example, let's say I enter this into the textarea:
dsfgsdfgdsfgdsg
line2
dsfgdfgdfg
line4
The PHP code that writes this to the text file is:
$content = $_POST['accountMappings'];
$file = "../userlist.txt";
$Saved_File = fopen($file, 'w');
fwrite($Saved_File, $content);
fclose($Saved_File);
The text gets written to the text file, but it gets written like this:
dsfgsdfgdsfgdsg
line2
dsfgdfgdfg
line4
There is an extra line break after each line.
There is nothing else manipulating the code before being saved by the code you see above. Where are these extra blank lines coming from?
在将其写入文件之前,请尝试以下操作:
$content = preg_replace('#\r\n?#', "\n", $content);
The problem lies with line endings. You must use the solution provided by @angelcool.net which is $content = preg_replace('#\\r\\n?#', "\\n", $content);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.