Java class/method similar to nl2br in PHP

R

rick.huby

Does Java have an equivalent of the rather handy PHP function nl2br
which replaces new lines with <br /> for nice use in websites?

I am sure it is nice and easy to just replace all of the \n or \r\n 's
with the <BR /> but with it being such a useful PHP thing I thought
that their might be a java version.

Thanks,
 
G

Gerbrand van Dieijen

(e-mail address removed) schreef:
Does Java have an equivalent of the rather handy PHP function nl2br
which replaces new lines with <br /> for nice use in websites?

I am sure it is nice and easy to just replace all of the \n or \r\n 's
with the <BR /> but with it being such a useful PHP thing I thought
that their might be a java version.

Thanks,

You could use "my text with with \n".replaceAll("\n","<BR />")
 
G

Gerbrand van Dieijen

(e-mail address removed) schreef:
Does Java have an equivalent of the rather handy PHP function nl2br
which replaces new lines with <br /> for nice use in websites?

I am sure it is nice and easy to just replace all of the \n or \r\n 's
with the <BR /> but with it being such a useful PHP thing I thought
that their might be a java version.

Thanks,

You could use "my text with with \n".replaceAll("\n","<BR />")

Or wrap it in a function (not tested):

public String nl2br(String text) {
return text.replaceAll("\n","<BR />");
}
 
R

rick.huby

Cheers. Was really just wondering if there was an actual function.

Whilst I prefer Java as a language, I do love some of the handy little
built in functions of PHP - I can certainly see why it is so popular!

Thanks,
 
G

Gerbrand van Dieijen

(e-mail address removed) schreef:
Cheers. Was really just wondering if there was an actual function.

Whilst I prefer Java as a language, I do love some of the handy little
built in functions of PHP - I can certainly see why it is so popular!

The Java designers already added a lot of function in the default
packages, but they didn't want the language to be 'polluted'
You can find on the net a lot of free and commercial packages for Java
than can do very useful or amazing things.
See for example http://www.java.net
 
Joined
Sep 30, 2010
Messages
1
Reaction score
0
Use a custom tag.

Hi,

I had the same problem. I though I'd post my code as I haven't found the solution elsewhere, and I expect it's a very common problem. I'm not saying it's the best code in the world, but it works.

My solution was to use a custom tag with a parameter. I'm assuming that most people are trying to get this to work in a JSP view, and would prefer to avoid scripting.

Given that I'm using a custom tag, I created my TLD and stuck it in WEB-INF:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" ...... etc. your IDE should do this bit for you ......>
  <tlib-version>1.0</tlib-version>
  <short-name>foo</short-name>
  <uri>/WEB-INF/foo</uri>  
  <tag>
    <name>nl2br</name>
    <tag-class>foo.bar.NewLine2PageBreak</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
      <name>text</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
  </tag>
</taglib>

Now you need your tag handler class:

Code:
public class NewLine2PageBreak extends SimpleTagSupport {
    private String text;

    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        try {
            out.println(Utils.nl2br(text));
        } catch (java.io.IOException ex) {
            throw new JspException("Error in NewLine2PageBreak tag", ex);
        }
    }

    public void setText(String text) {
        this.text = text;
    }

}

You'll notice I'm calling a static utility method from my Utils class. You don't have to separate it out like this, of course.

Code:
    public static String nl2br(String myString) {        
        return myString.replaceAll(getLineSeparator(), "<br/>");
    }

    public static String getLineSeparator() {
        return System.getProperty("line.separator");
    }

You need a reference to the taglib in the JSP:

Code:
<%@taglib uri="/WEB-INF/foo" prefix="foo" %>

And this is how to use the custom tag:

Code:
<foo:nl2br text="${yourText}"/>

I hope that is of use to someone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top