473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
6 62909
"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

"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
"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher Glaeser | last post by:
Consider the code: $arrText = file ("http://www.site.com/page.htm"); for ($i=0; $i<count($arrText); i++) { echo("$arrText\n"); } (I did not test the snippet for errors) The line breaks...
0
by: Jonathan | last post by:
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...
4
by: intl04 | last post by:
I have a memo field that is included in some Access reports I created. Is there some way for the memo field to display nicely formatted text, with line breaks between paragraphs? Or is it necessary...
0
by: Peter Rilling | last post by:
Although an ADODB question, I am using C#, so here I go. I am trying to use the ADODB Stream object to extract content from a body part of an MHTML formatted document. I can save the raw...
5
by: joelbyrd | last post by:
Didn't know exactly where to post this, but: How do I get line breaks in a textarea? I'm pulling text from a database, and this text definately has line breaks in it, because I replaced all the...
8
by: Juan Puebla | last post by:
Hi, I have an sql database with some text (nvarchar) fields. The problem is that if I get those fields with a datareader (dr.getstring(0)) an then I save text in a txt file I loose all the line...
14
by: ghostwalker | last post by:
Hi I have an HTML form with a textarea on it. When submitted (using 'get' not 'post') this forms action php file simply does this to retrieve the values: $message = $_GET; Now it all works...
1
by: Arielle | last post by:
Trying to migrate from ASP classic to ColdFusion and was wondering how to handle this. A form has a text area that submits to a database and stores a memo that can later be viewed. How can I...
4
by: Steve Marson | last post by:
I am using a simple form in modx CMS and I need the textarea to retain the line breaks. I'd prefer a javascript solution if possible. This is because the textarea will be used to order food and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.