How to use the function nl2br

Posted on 2006-04-28
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

    Author Comment

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

    LVL 25

    Assisted Solution

    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


    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

    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

    Course: Create Mobile App Prototypes with Adobe XD

    This is a project-based course: we go through all the steps of creating a prototype from start to finish, using all the tools and features currently available in Adobe XD. You can complete the course in less than a day, plus all project files and fonts are included.

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now