phpjs
Version:
103 lines (84 loc) • 4.24 kB
Markdown
---
layout: page
title: "JavaScript wordwrap function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/wordwrap:581
- /functions/view/wordwrap
- /functions/view/581
- /functions/wordwrap:581
- /functions/581
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's wordwrap
{% codeblock strings/wordwrap.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/wordwrap.js raw on github %}
function wordwrap (str, int_width, str_break, cut) {
// From: http://phpjs.org/functions
// + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Nick Callen
// + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Sakimori
// + bugfixed by: Michael Grier
// * example 1: wordwrap('Kevin van Zonneveld', 6, '|', true);
// * returns 1: 'Kevin |van |Zonnev|eld'
// * example 2: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n');
// * returns 2: 'The quick brown fox <br />\njumped over the lazy<br />\n dog.'
// * example 3: wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');
// * returns 3: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim \nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \ncommodo consequat.'
// PHP Defaults
var m = ((arguments.length >= 2) ? arguments[1] : 75);
var b = ((arguments.length >= 3) ? arguments[2] : "\n");
var c = ((arguments.length >= 4) ? arguments[3] : false);
var i, j, l, s, r;
str += '';
if (m < 1) {
return str;
}
for (i = -1, l = (r = str.split(/\r\n|\n|\r/)).length; ++i < l; r[i] += s) {
for (s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")) {
j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
}
}
return r.join("\n");
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/wordwrap.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/wordwrap.js)
### Example 1
This code
{% codeblock lang:js example %}
wordwrap('Kevin van Zonneveld', 6, '|', true);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'Kevin |van |Zonnev|eld'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'The quick brown fox <br />\njumped over the lazy<br />\n dog.'
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim \nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \ncommodo consequat.'
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}