web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #4  
Old 04-17-2012, 11:07 AM
upstart33 upstart33 is offline
Mirth Guru
 
Join Date: Dec 2010
Location: Chicago, IL.
Posts: 459
upstart33 is on a distinguished road
Default

4. Word-Wrap
Purpose: If for some reason, you need to perform some Work Wrapping, then this function emulates PHP's 'wordwrap'.

-The string to be wrapped.
-The column width (a number, default: 75)
-The character(s) to be inserted at every break. (default: \n)
-The cut: a Boolean value (false by default).

Code:
function wordwrap( str, width, brk, cut ) {
 
  brk = (brk ? brk :\n);
  width = (width ? width : 75);
  cut = (cut ? cut : false);
 
    if (!str) { return str; }
 
    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
 
    return str.match( RegExp(regex, 'g') ).join( brk );
 
}
The function can be called by:
Code:
wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br/>\n');
---------
The quick brown fox <br/>
jumped over the lazy <br/>
dog.
Reply With Quote