How to use the function nl2br

Posted on 2006-04-28
Medium Priority
Last Modified: 2012-08-13
my problem is that when I save an essay into the MS sql database which is a varchar field it loses all the formatting tags. While searching in EE I found that many peple had used nl2br function to solve this problem but I still don't understand how to use this.

Ay help will be greatly appreciated
Question by:royalcyber
  • 2

Author Comment

ID: 16565925
By the way I am using java as my backend programming language

LVL 25

Assisted Solution

InteractiveMind earned 500 total points
ID: 16566116
nl2br merely replaces all new lines ("line breaks") with <br /> (or simply <br>) tags.

In Java, you could use the String class's replaceAll() method, which is available in 1.4 up if I recall correctly.

    public static String nl2br( String html )
        return html.replaceAll( "\n", "<br />" ) ;

Give this a go.
But based on what you've told us, I suspect that you're going to require a little more than this..
LVL 11

Accepted Solution

WelkinMaze earned 500 total points
ID: 16566968

Also you may have to use the reverse function after reading the text from the database in order to convert it in its previous state:

    public static String br2nl( String html )
        return html.replaceAll( "<br />", "\n" ) ;

Author Comment

ID: 16607048
Thnak you very much guys

But I actually found a better option, which is using javascript to create a formatting  toolbar like microsoft word and allow users to automatically add the tags
Please check out this website


Thanks but your answers also worked



Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Loops Section Overview

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question