Return to Snippet

Revision: 721
at August 2, 2006 09:42 by cochambre


Updated Code
function nl2br(text){
	text = escape(text);
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}
	return unescape( text.replace(re_nlchar,'<br />') );
}

Revision: 720
at August 2, 2006 09:00 by cochambre


Initial Code
function nl2br(text){
	text = escape(text);// encode string's carriage returns
	for(i = 0; i < text.length; i++){
		//loop through string, replacing carriage return encoding with HTML break tag
		if(text.indexOf('%0D%0A') > -1){ //Windows encodes returns as \\r\\n hex
			text = text.replace('%0D%0A','<br />');
		}else if(text.indexOf('%0A') > -1){ //Unix encodes returns as \\n hex
			text = text.replace('%0A','<br />');
		}else if(text.indexOf('%0D') > -1){ //Macintosh encodes returns as \\r hex
			text = text.replace('%0D','<br />');
		}
		return unescape(text); //unescape all other previously encoded characters
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
Replace newlines with BR (platform safe)

Initial Tags
html, replace, line

Initial Language
JavaScript