0

I'm trying to do a forum in php. I have a problem. I have a publish form with a textarea. Let's imagine that I fill it all.

When I make a line break (when I press space bar), with nl2br, in my database there are <br> tags added. But when I just type and there is an auto line break, there is no <br>.

How can I do to, for example, at the end of 100 chars, there is an auto line break (that adds a <br> in the database. I've tried in Javascript but I didn't found a correct answer.

With

with <br>

In the database

Without

auto line break in textarea (no <br>)

in the database

Problem:

in the forum (text outside of the div

IMSoP
  • 89,526
  • 13
  • 117
  • 169
twinex
  • 1
  • Don't convert line breaks to `
    ` before you save the data in your database. Store the data as is and add any necessary changes when you output the data instead. You might want to display the data in some other way later, then it's better if it's clean in the database, or you will need to first clean the data and then format it for the new display.
    – M. Eriksson Jun 04 '22 at 08:56
  • The phrase you probably want to search for is "line wrapping". – IMSoP Jun 04 '22 at 08:57
  • The space bar doesn't add line breaks, it adds spaces. What you're seeing is simply the text being too wide for the container (in your case, the textarea) so it _wraps_ the text in the end and continues on the next line. But that's just for displaying the text correctly in the component, it doesn't actually add anything to the text. – M. Eriksson Jun 04 '22 at 09:01
  • Thank you very much for your answers, I'm going to try this now! – twinex Jun 04 '22 at 09:05

1 Answers1

-1

First part about PHP, I am also creating PHP website now but it's social media, and here how I did to make line breaks and also to escape chars like " or \ in my chat.

$letter = join("", explode("\r\n", $letter));
$letter = join(" \\\\ ", explode("\\", $letter));
$letter = join("\\\"", explode("\"", $letter));
$stmt = $mysqli->prepare("INSERT INTO Chat (sender, recipient, msg, date) VALUE (?, ?, ?, ?)");
$stmt->bind_param("ssss", $sender, $recipient, $letter, $dateofletter);
//$conn->query($sql);
$stmt->execute();
$mysqli->close();

and about the text too long try these

word-wrap: break-word;
word-break: break-all;

But if you want to exactly have the same line-breaks which user typed, then simply don't use CSS word break and replace write this $letter = join("<br>", explode("\r\n", $letter)); instead of $letter = join("", explode("\r\n", $letter));

or you may need $letter = join(htmlspecialchars("<br>"), explode("\r\n", $letter));

Sevada 797
  • 346
  • 1
  • 8
  • Also you don't need to do line breaks with
    or try doing it with JavaScript, as it is client side code, and can be changed by everyone, if you want your code being safe always do that kind of things in server-side, do it with PHP, you just can replace all \r\n with "", and after add to your css word-wrap: break-word, that's it.
    – Sevada 797 Jun 04 '22 at 09:09
  • And how does it answer the question? You didn't "make" any line breaks you simply removing them – Your Common Sense Jun 04 '22 at 10:06
  • well it doesn't matter if you need result, this is a way, yes I removed the line breaks, but if you want code that will replace line-breaks with html line breaks it's not that hard, simply $letter = join("
    ", explode("\r\n", $letter)); instead of $letter = join("", explode("\r\n", $letter));
    – Sevada 797 Jun 04 '22 at 10:47
  • Then what am I saying is to not convert line breaks to
    , and after if you use the PHP code I wrote it will work fine
    – Sevada 797 Jun 04 '22 at 11:42
  • `join(htmlspecialchars("
    ")` makes **absolutely** no sense. You don't even understand the code you write
    – Your Common Sense Jun 04 '22 at 12:07
  • I wrote that for security, doesn't it make xss attack chances low? – Sevada 797 Jun 04 '22 at 13:00