Created
July 31, 2015 13:39
-
-
Save ReeMii/a739931164e2dee7a5f8 to your computer and use it in GitHub Desktop.
nl2br
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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