Skip to content

Instantly share code, notes, and snippets.

@ronisaha
Created February 8, 2017 05:54
Show Gist options
  • Select an option

  • Save ronisaha/45a1498d960251ea2d38cd96c16bca62 to your computer and use it in GitHub Desktop.

Select an option

Save ronisaha/45a1498d960251ea2d38cd96c16bca62 to your computer and use it in GitHub Desktop.
PHP_WORDWRAP in javascript
function wordwrap( str, width, brk, cut ) {
brk = brk || '\n';
width = width || 75;
cut = cut || false;
if (!str) { return str; }
var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
return str.match( new RegExp(regex, 'g') ).join( brk );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment