JavaScript chunk_split
Returns split line
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 | function chunk_split (body, chunklen, end) { // Returns split line // // version: 1008.1718 // discuss at: http://phpjs.org/functions/chunk_split // + original by: Paulo Freitas // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Theriault // * example 1: chunk_split('Hello world!', 1, '*'); // * returns 1: 'H*e*l*l*o* *w*o*r*l*d*!*' // * example 2: chunk_split('Hello world!', 10, '*'); // * returns 2: 'Hello worl*d!*' chunklen = parseInt(chunklen, 10) || 76; end = end || '\r\n'; if (chunklen < 1) { return false; } return body.match(new RegExp(".{0," + chunklen + "}", "g")).join(end); } |
Examples
» Example 1
Running
1 | chunk_split('Hello world!', 1, '*'); |
Should return
1 | 'H*e*l*l*o* *w*o*r*l*d*!*' |
» Example 2
Running
1 | chunk_split('Hello world!', 10, '*'); |
Should return
1 | 'Hello worl*d!*' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have chunk_split goodness in JavaScript.

@ Paulo Ricardo F. Santos: Ignore that. It works beautifully ;)
Paulo Ricardo F. Santos
1 Dec '08