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
Without
auto line break in textarea (no <br>)
Problem:
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));