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 7560003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:47:07+00:00 2026-05-30T12:47:07+00:00

I have a system set up for users to submit their articles into my

  • 0

I have a system set up for users to submit their articles into my database. Since it will just be HTML, I don’t want to expect them to know to type <br /> every time there’s a newline, so I am using the PHP function nl2br() on the input.

I’m also providing an article modification tool, which will bring their articles back into the form (this is a different page, however) and allow them to edit it. In doing this, the <br /> elements were appearing also (with newlines still). To remedy the
elements appearing (which I had expected, anyway) I added preg_replace('/<br(\s+)?\/?>/i', "\n", mysql_result($result,$i,"content")) which I had found in another question on this site. It does the job of removing the <br /> elements, but since it is replacing them with newlines, and the newlines would have remained originally anyway, every time the post is edited, more and more newlines will be added, spacing out the paragraphs more and more each time. This is something a user won’t understand.

As an example, say I enter the following into the article submission form:

 Hello, this is my article.
 I am demonstrating a new line here.

This will convert to:

Hello, this is my article.<br />
I am demonstrating a new line here.

Notice that, even though the newline character was converted, there is still a newline in the text. In the editing form, the <br /> will be converted back to newline and look like this:

Hello, this is my article.

I am demonstrating a new line here.

Because the <br /> was converted to a newline, but there was already a newline. So I guess what I’m expecting is for it to originally be converted to something like this:

Hello, this is my article.<br />I am demonstrating a new line here.

I’m wondering … is there a way to stop the nl2br() function from maintaining the original newlines? Might it have to do with the Windows \r\n character?

  • 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-30T12:47:08+00:00Added an answer on May 30, 2026 at 12:47 pm

    The function you’re using, nl2br is used for inserting them, but not replacing them. If you want to replace \n with <br /> you just need to use str_replace. Like so:

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

    There is absolutely no need for regex in this situation.

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

Sidebar

Related Questions

I have a user system set up in a 'users' table, and I have
I have a system set up to lock certain content in a database table
I have a potential client that set up their website and membership system in
I have a credit system set up on my site where user A can
We have set up a system where notifications get sent to a user with
I have a system in place which applies calculations to a set of numbers
I have set up a version control system using TortoiseSVN at my home to
I have a set of Visual Studio Team System unit (integration really) tests that
I have a set of Berkeley DB files on my Linux file system that
How should I set WordWrap = false to a System.Windows.Forms.Label ? I have 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.