0

I have some user submitted data that comes from a <textarea>. I then have some code that inserts a comment above it. Kind of like this....

$my_comment = 'stuff from me';
$user_comment = $_POST['user_comment'];
$full_comment = $my_comment . '\n\n' . $user_comment;

After this, $full_comment gets inserted into the DB. Later, it gets pulled from the DB and put into a <textarea>. I want the \n\n to actually function like new lines, but instead they actually show as "\n\n" in the new <textarea>.

How can I make the \n\n function correctly?

BTW, I already know about form validation, PDO, etc. This is just example code.

gtilflm
  • 1,389
  • 1
  • 21
  • 51
  • 4
    If you use single quotes it won't be interpreted correctly. Use double quotes: `"\n\n"` – ishegg Feb 25 '18 at 01:49
  • That was it @ishegg! Thanks! – gtilflm Feb 25 '18 at 01:50
  • Instead of using `\n`, why don't you go with the official PHP end of line? `$full_comment = $my_comment . PHP_EOL . PHP_EOL . $user_comment;` – Alejandro Iván Feb 25 '18 at 01:53
  • @AlejandroIván depends on the use case, a browser for example will collapse the newline (unless you wrap the code in `
    ` tags) so it's no use (buena weón!)
    – ishegg Feb 25 '18 at 01:59
  • @ishegg right, but on textarea I’m not sure if it actually works that way (weeena!) – Alejandro Iván Feb 25 '18 at 02:09
  • @AlejandroIván you're completely right, I stopped reading after '\n\n' lol, `PHP_EOL` definitely does work as intended inside `textarea` – ishegg Feb 25 '18 at 02:10
  • Didn't know about `PHP_EOL`. Thanks for the tip. – gtilflm Feb 25 '18 at 02:11
  • @ishegg: Want to post a quick answer so you can get credit? – gtilflm Feb 25 '18 at 02:18
  • No worries man, I'm not here for the points :). In fact, since people here love to downvote everything, maybe you can delete your question? Since your problem is resolved. Good luck! – ishegg Feb 25 '18 at 02:19
  • I'm actually going to leave it b/c it is, in fact, not a duplicate. This would be valuable for people who don't know about the single vs. double quote thing for `\n`. The claimed duplicate is someone specifically asking if you can single quote it. Thx for posting your answer! – gtilflm Feb 25 '18 at 02:24

1 Answers1

0

You can use htmlentities functions.

$full_comment = $my_comment . '\n\n' . $user_comment;
$full_comment =  htmlentities( $full_comment );

also stripslashes can be used to strip slashed if necessary

$full_comment =  stripslashes( htmlentities( $full_comment ) );
madhushankarox
  • 1,448
  • 1
  • 13
  • 17