note 30113 deleted from function.str-replace by pollita
| From: | pollita@php.net | Date: | Sun, 11 Jan 2004 19:07:52 +0000 |
| Subject: | note 30113 deleted from function.str-replace by pollita | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63323@lists.php.net to get a copy of this message | ||
Note Submitter: monkey@greyledge.net
----
After much hair-pulling I think I've found a way to consistently maintain newline formatting
within an HTML form textarea tag.
The problem I was having was with a text editing 'preview' function, where the contents of
the textarea would be passed to another textarea after form submission: each time I would enter some
new text and hit the 'preview' function, any linebreaks in the textarea would be doubled
on the next page.
I knew this had to a conflict related to the MS-DOS style "\r\n" linebreak sequence, and I
wrote a simple function to normalize newline sequences in a chunk of text to use only Unix-style
'\n' linefeeds:
function normalizeNewline($text){
/* fix DOS newlines */
str_replace("\r\n", "\n", $text);
/* fix Mac newlines */
str_replace("\r", "\n", $text);
return $text;
}
First I tried using this function on text after it was submitted, but those extra newlines would
still appear if I plugged the submitted text back into a textarea.
I finally figured out that I have to run this function on text both *before* displaying it in a
textarea and again *after* the textarea is submitted in order to preserve the correct number of line
breaks in a chunk of text. Hope this helps anyone who might have had the same problem.