Skip to content

Instantly share code, notes, and snippets.

@ReeMii
Created July 31, 2015 13:39
Show Gist options
  • Save ReeMii/a739931164e2dee7a5f8 to your computer and use it in GitHub Desktop.
Save ReeMii/a739931164e2dee7a5f8 to your computer and use it in GitHub Desktop.
nl2br
function nl2br(str, is_xhtml) {
// discuss at: http://phpjs.org/functions/nl2br/
// original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Philip Peterson
// improved by: Onno Marsman
// improved by: Atli Þór
// improved by: Brett Zamir (http://brett-zamir.me)
// improved by: Maximusya
// bugfixed by: Onno Marsman
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// input by: Brett Zamir (http://brett-zamir.me)
// example 1: nl2br('Kevin\nvan\nZonneveld');
// returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
// example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
// returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
// example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
// returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>'; // Adjust comment to avoid issue on phpjs.org display
return (str + '')
.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment