Replace \r\n with the (br) tag

From CodeCodex

Implementations[edit]

Haskell[edit]

replaceRN [] = []
replaceRN ('\r':'\n':xs) = "<br>" ++ replaceRN xs
replaceRN ('\n':'\r':xs) = "<br>" ++ replaceRN xs
replaceRN ('\n':xs) = "<br>" ++ replaceRN xs
replaceRN ('\r':xs) = "<br>" ++ replaceRN xs
replaceRN (x:xs) = x : replaceRN xs

Java[edit]

This can be done easily a regular expression. [JDK1.4]

import java.util.regex.Pattern;
import java.util.regex.Matcher;

...
// 4 different combinaisons
Pattern CRLF = Pattern.compile("\r\n|\r|\n|\n\r");
Matcher m = CRLF.matcher(myString);
 
newString = m.replaceAll("<br>");

or use the String.replaceAll(regex,replacement) method which is doing basically the same thing.

newString = myString.replaceAll("\r\n|\r|\n|\n\r", "<br>");

Ruby[edit]

string = "abc\ndef\rghi\r\njkl\n\rmno"
crlf = /(\r\n|\n\r|\r|\n)/
newstring = string.gsub(crlf, "<br>")  #=> "abc<br>def<br>ghi<br>jkl<br>mno"