Follow Me

How to preserve line breaks in textarea mysql data

Thu, Sep 23, 2010

Html, PHP

We often use textarea to insert multiple lines of data into database. While writing content to textarea we use line breaks.  At the time of display if we simply echo text area content then it does not preserve line breaks and stick all of the data together.

There are two ways we can preserve line breaks mysql data -

1. Use nl2br() function

Returns string with ‘<br />’ or ‘<br>’ inserted before all newlines.

Example

<div>
<div><code> <?php
echo nl2br($row["myarea"]);
?> </code></div>
</div>

2. Use <pre>textarea contents</pre> tag for display data. Put your textarea database contents inside this tag and it preserves line breaks in formatted text style.

Example

<?php
echo "<pre>".$row["myarea"]."</pre>";
?>

You can use any method alternatively. Output from both methods shows difference clearly in screenshot.

preserve-line-break-in-inserted-textarea-data-display

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • 1 Comments For This Post

    1. Sandeep Says:

      Thanks a lot.

      I am very very thankful to you.

      Regards,
      Sandeep

    Leave a Reply