(JS) Newline to br


Replaces newline characters with <br> tags in the same style as the php nl2br function

// newline to br - php style
function nl2br(str, is_xhtml) {
    let breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *