424,054 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,054 IT Pros & Developers. It's quick & easy.

How to handle line breaks when storing textarea entry in a database?

P: n/a
I want to save textarea contents to a mysql database with the
paragraph breaks intact without having to type paragraph or break tags
in HTML. How can I do that. So far, although it occurs naturally when
I save the contents to a file, it doesn't break up the paragraphs
properly when I save it to my database.

Thanks!

Jonathan
http://thewebdevelopment.com
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Jonathan" wrote:
I want to save textarea contents to a mysql database with the
paragraph breaks intact without having to type paragraph or break tags in HTML. How can I do that. So far, although it occurs naturally when I save the contents to a file, it doesn’t break up the
paragraphs
properly when I save it to my database.

Thanks!

Jonathan
http://thewebdevelopment.com


Jonathan, textarea uses cr\lf to break lines. That is how it would
be saved into db, if your program does no extra translations. Upon
display on html page, cr\lf could then be replaced by html break
tags.

I don’t know of paragraph breaks. I believe that is simply double
cr\lf’s.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/mySQL-handle...ict131179.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=438213
Jul 20 '05 #2

P: n/a

"Jonathan" <th***************@yahoo.com> wrote in message
news:67**************************@posting.google.c om...
I want to save textarea contents to a mysql database with the
paragraph breaks intact without having to type paragraph or break tags
in HTML. How can I do that. So far, although it occurs naturally when
I save the contents to a file, it doesn't break up the paragraphs
properly when I save it to my database.

You need to replace your \r\n with <p />
and your \n with <br />

php code sample (handles dos and *nix text):

$string = ereg_replace("(\r\n\r\n|\n\n|\r\r)", "<p />", $_POST['testtext']);

^^^^^^^^^^^^^

Text from query

$string = stripcslashes(ereg_replace("(\r\n|\n|\r)", "<br />", $string));

print $string;
Jul 20 '05 #3

P: n/a
"Jonathan" wrote:
I want to save textarea contents to a mysql database with the
paragraph breaks intact without having to type paragraph or break tags in HTML. How can I do that. So far, although it occurs naturally when I save the contents to a file, it doesn’t break up the
paragraphs
properly when I save it to my database.

Thanks!

Jonathan
http://thewebdevelopment.com


Jonathan, textarea uses cr\lf to break lines. That is how it would
be saved into db, if your program does no extra translations. Upon
display on html page, cr\lf could then be replaced by html break
tags.

I don’t know of paragraph breaks. I believe that is simply double
cr\lf’s.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/mySQL-handle...ict131179.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=438213
Jul 20 '05 #4

P: n/a

"Jonathan" <th***************@yahoo.com> wrote in message
news:67**************************@posting.google.c om...
I want to save textarea contents to a mysql database with the
paragraph breaks intact without having to type paragraph or break tags
in HTML. How can I do that. So far, although it occurs naturally when
I save the contents to a file, it doesn't break up the paragraphs
properly when I save it to my database.

You need to replace your \r\n with <p />
and your \n with <br />

php code sample (handles dos and *nix text):

$string = ereg_replace("(\r\n\r\n|\n\n|\r\r)", "<p />", $_POST['testtext']);

^^^^^^^^^^^^^

Text from query

$string = stripcslashes(ereg_replace("(\r\n|\n|\r)", "<br />", $string));

print $string;
Jul 20 '05 #5

P: n/a
steve wrote:
"Jonathan" wrote:
> I want to save textarea contents to a mysql database with the
> paragraph breaks intact without having to type paragraph or break

tags
> in HTML. How can I do that. So far, although it occurs naturally

when
> I save the contents to a file, it doesn’t break up the
> paragraphs
> properly when I save it to my database.
>
> Thanks!
>
> Jonathan
> http://thewebdevelopment.com


Jonathan, textarea uses cr\lf to break lines. That is how it would
be saved into db, if your program does no extra translations. Upon
display on html page, cr\lf could then be replaced by html break
tags.

I don’t know of paragraph breaks. I believe that is simply double
cr\lf’s.

I would like to suggest a small correction:

A 'paragraph break' is actually CR/LF. A textarea will break lines using
wordwrap. (To some people a paragraph break is known as a 'hard break':
its hard-coded, whereas a wordwrap is a 'soft break': software
calculates it.) A wordwrap doesn't actually use any symbol wthin the
stored text, since the position of the line breaks will alter depending
on the media in which the text is displayed.

HTML also uses wordwrap, but it ignores CR/LF. In its place you can use
either a <BR> for linebreak or <p> for paragraphs. If you have a block
of text which uses CR/LFs that you wish to display in HTML, then you'll
need to substitute <br> for each of the CR/LFs. (in PHP there is a
function nl2br() that does this.)

History: CR/LF goes back to the days of early printers where the codes
replicated the actions of a typewriter. CR (carriage return) returned
the carriage to the start of a new line. LF (line feed) advanced to the
next line. The lever at the left end of a typewriter combined moving
the carriage and advancing the line. Typists used to apply a CR/LF at
the end of every line and an extra CR/LF between paragraphs. As electric
typewriters came into use they would automatically apply the CR/LF when
the end of the line was reached.

Now with wordprocessing and wordwrap, the CR/LF at the end of each line
is no longer neccessary.

Any corrections/improvements to this explanation would be greatfully
received.

Mike
Jul 20 '05 #6

P: n/a
steve wrote:
"Jonathan" wrote:
> I want to save textarea contents to a mysql database with the
> paragraph breaks intact without having to type paragraph or break

tags
> in HTML. How can I do that. So far, although it occurs naturally

when
> I save the contents to a file, it doesn’t break up the
> paragraphs
> properly when I save it to my database.
>
> Thanks!
>
> Jonathan
> http://thewebdevelopment.com


Jonathan, textarea uses cr\lf to break lines. That is how it would
be saved into db, if your program does no extra translations. Upon
display on html page, cr\lf could then be replaced by html break
tags.

I don’t know of paragraph breaks. I believe that is simply double
cr\lf’s.

I would like to suggest a small correction:

A 'paragraph break' is actually CR/LF. A textarea will break lines using
wordwrap. (To some people a paragraph break is known as a 'hard break':
its hard-coded, whereas a wordwrap is a 'soft break': software
calculates it.) A wordwrap doesn't actually use any symbol wthin the
stored text, since the position of the line breaks will alter depending
on the media in which the text is displayed.

HTML also uses wordwrap, but it ignores CR/LF. In its place you can use
either a <BR> for linebreak or <p> for paragraphs. If you have a block
of text which uses CR/LFs that you wish to display in HTML, then you'll
need to substitute <br> for each of the CR/LFs. (in PHP there is a
function nl2br() that does this.)

History: CR/LF goes back to the days of early printers where the codes
replicated the actions of a typewriter. CR (carriage return) returned
the carriage to the start of a new line. LF (line feed) advanced to the
next line. The lever at the left end of a typewriter combined moving
the carriage and advancing the line. Typists used to apply a CR/LF at
the end of every line and an extra CR/LF between paragraphs. As electric
typewriters came into use they would automatically apply the CR/LF when
the end of the line was reached.

Now with wordprocessing and wordwrap, the CR/LF at the end of each line
is no longer neccessary.

Any corrections/improvements to this explanation would be greatfully
received.

Mike
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.