Re: carriage return/line break problem
| From: | Richard Lynch | Date: | Sun, 25 Aug 2002 00:40:26 +0000 |
| Subject: | Re: carriage return/line break problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-114010@lists.php.net to get a copy of this message | ||
> I am having a problem with line breaks in some text i am keeping on a
>database.
> I add a record to an existing database, through a form, using the [return]
>key as i want to leave blank lines. It saves ok, but when i retrieve the
>text from the db and put it on a page, the line breaks dont exist, the text
>is all straight.
> But if i chose to edit that field on a form, the line spaces are still there.
> What can i do to solve this problem? How can i display this field
>correctly on a page?
First, use <TEXTBOX WRAP=VIRTUAL ...> in your FORM, or you'll go crazy.
Without WRAP=VIRTUAL, the browser will add or remove line-breaks
willy-nilly. (Not really willy-nilly, but it will seem like it.)
Second, use "View Source" in your browser to see the line breaks.
Line breaks don't "count" in HTML.
You probably want to use http://php.net/nl2br when you display
your text in
a browser but not in the TEXTAREA.
Also be aware that Macintosh and Linux users' browsers may use just \r or
just \n for line-breaks.
You probably want to do something like this before you insert to the
database:
<?php
# Assume NAME=text was in your TEXTBOX:
# Force any Windoze line-breaks to Unix:
$text = str_replace("\r\n", "\n", $text);
# Force any Mac line-breaks to Unix:
$text = str_replace("\r", "\n", $text);
?>
The *order* of the str_replace lines is very important. You can't do the
Mac first and Unix second.
--
Like Music? http://l-i-e.com/artists.htm