Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1074959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:12:27+00:00 2026-05-16T21:12:27+00:00

<textarea> put returns between paragraphs for linebreak add 2 spaces at end indent code

  • 0
<textarea> put returns between paragraphs
for linebreak add 2 spaces at end
indent code by 4 spaces
quote by placing > at start of line
</textarea>

$text = value from this textarea;

How to:

1) Get each line from this textarea ($text) and work with them using foreach()?

2) Add <br /> to the end of each line, except the last one?

3) Throw each line to an array.

Important – text inside textarea can be multilanguage.


Have tried to use:

$text = str_replace('\n', '<br />', $text);

But it doesn’t work.


Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:12:28+00:00Added an answer on May 16, 2026 at 9:12 pm

    Warning: this answer is focused on the particular part of OP, which is not related to the question title. For the answer to the question "how to get each line", consider the posts below.

    You will want to look into the nl2br() function along with the trim().

    The nl2br() will insert <br /> before the newline character (\n) and the trim() will remove any ending \n or whitespace characters.

    $text = trim($_POST['textareaname']); // remove the last \n or whitespace character
    $text = nl2br($text); // insert <br /> before \n 
    

    That should do what you want.

    UPDATE

    The reason the following code will not work is because in order for \n to be recognized, it needs to be inside double quotes since double quotes parse data inside of them, where as single quotes takes it literally, IE "\n"

    $text = str_replace('\n', '<br />', $text);
    

    To fix it, it would be:

    $text = str_replace("\n", '<br />', $text);
    

    But it is still better to use the builtin nl2br() function, PHP provides.

    EDIT

    Sorry, I figured the first question was so you could add the linebreaks in, indeed this will change the answer quite a bit, as anytype of explode() will remove the line breaks, but here it is:

    $text = trim($_POST['textareaname']);
    $textAr = explode("\n", $text);
    $textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
    
    foreach ($textAr as $line) {
        // processing here. 
    } 
    

    If you do it this way, you will need to append the <br /> onto the end of the line before the processing is done on your own, as the explode() function will remove the \n characters.

    Added the array_filter() to trim() off any extra \r characters that may have been lingering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add different styles within a textarea eg bold, different colors
I have a textarea with many lines of input, and a JavaScript event fires
I create a TextArea in actionscript: var textArea:TextArea = new TextArea(); I want it
How does Stackoverflow implement the resizable textarea? Is that something they rolled themselves or
I have saved input from a textarea element to a TEXT column in MySQL.
I have a form with a textarea. Users enter a block of text which
Is there a simple way of getting a HTML textarea and an input type=text
How can I change the width of a textarea form element if I used
On a site of mine in which a textarea is used for submission, I
I would like to print only the contents of a textarea element from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.