Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

nl2br javascript

function nl2br (str, is_xhtml) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: Philip Peterson
  // +   improved by: Onno Marsman
  // +   improved by: Atli Þór
  // +   bugfixed by: Onno Marsman
  // +      input by: Brett Zamir (http://brett-zamir.me)
  // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // +   improved by: Maximusya
  // *     example 1: nl2br('Kevin
van
Zonneveld');
  // *     returns 1: 'Kevin<br />
van<br />
Zonneveld'
  // *     example 2: nl2br("
One
Two

Three
", false);
  // *     returns 2: '<br>
One<br>
Two<br>
<br>
Three<br>
'
  // *     example 3: nl2br("
One
Two

Three
", true);
  // *     returns 3: '<br />
One<br />
Two<br />
<br />
Three<br />
'
  var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>'; // Adjust comment to avoid issue on phpjs.org display

  return (str + '').replace(/([^>
]?)(
|

|
|
)/g, '$1' + breakTag + '$2');
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript
ADD COMMENT
Topic
Name
5+4 =