(JS) Newline to br

By | February 4, 2016

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

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

Leave a Reply

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