Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
How to make line breaks when displaying the content of a textarea?

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!