1

Possible Duplicate:
line breaks in a textarea

I have a text area where the user inputs some description in paragraphs. Now I want that if the user wants to add two paragraph, he will press enter to add a line break. But when we store that value in database then that line break does not get saved.

I want to ask that if a user press enter to add a line break then it should also get saved in the database.

I m using php.

Thanks

Community
  • 1
  • 1
user974172
  • 75
  • 2
  • 8

3 Answers3

5

One line break should be a <br /> and two should signify a paragraph break. So just look for your two "\n\n" statements in your code and replace all those with </p><p> and then just wrap <p></p> tags around the entire string.

Here's the series of steps that should take place:

  1. Submit to your PHP server and set $input = $_POST['input'];
  2. Do $input = trim($input) to remove all the extra linebreaks and whitespace from the start and end of the input.
  3. Then do $input = str_replace($input, "\n\n",'</p><p>');
  4. Then do $input = '<p>' . $input . '</p>';

---- EDIT ----

One final thing to keep in mind. If you do not want to have a bunch of empty <p></p> tags when If a user enters more than two consecutive linebreaks then you need to look out for that.

To get around that, do this instead.

$input = preg_replace("/\n(?:\n)+/",'</p><p>', $input)
//you may need to set the PHP PCRE multiline flag for this to work
matsko
  • 21,895
  • 21
  • 102
  • 144
1

You can use mysql_real_escape_string($input); This would probably save line feeds as well.

aceph ali
  • 57
  • 4
0

use nl2br($content_here); while retrieving

http://php.net/manual/en/function.nl2br.php

Sujeet
  • 1,780
  • 3
  • 16
  • 33