note 30113 added to function.str-replace
| From: | monkey at greyledge dot net | Date: | Thu, 06 Mar 2003 19:33:45 +0000 |
| Subject: | note 30113 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44950@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/function.str-replace.php
http://master.php.net/manage/user-notes.php?action=edit+30113
http://master.php.net/manage/user-notes.php?action=delete+30113
http://master.php.net/manage/user-notes.php?action=reject+30113