UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

72 lines (61 loc) 2.2 kB
--- layout: page title: "JavaScript str_split function" comments: true sharing: true footer: true alias: - /functions/view/str_split:530 - /functions/view/str_split - /functions/view/530 - /functions/str_split:530 - /functions/530 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's str_split {% codeblock strings/str_split.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/str_split.js raw on github %} function str_split (string, split_length) { // From: http://phpjs.org/functions // + original by: Martijn Wieringa // + improved by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Onno Marsman // + revised by: Theriault // + input by: Bjorn Roesbeke (http://www.bjornroesbeke.be/) // + revised by: Rafał Kukawski (http://blog.kukawski.pl/) // * example 1: str_split('Hello Friend', 3); // * returns 1: ['Hel', 'lo ', 'Fri', 'end'] if (split_length === null) { split_length = 1; } if (string === null || split_length < 1) { return false; } string += ''; var chunks = [], pos = 0, len = string.length; while (pos < len) { chunks.push(string.slice(pos, pos += split_length)); } return chunks; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/str_split.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/str_split.js) ### Example 1 This code {% codeblock lang:js example %} str_split('Hello Friend', 3); {% endcodeblock %} Should return {% codeblock lang:js returns %} ['Hel', 'lo ', 'Fri', 'end'] {% endcodeblock %} ### Other PHP functions in the strings extension {% render_partial _includes/custom/strings.html %}