Use PHP functions in JavaScript

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: 911.2923
    // discuss at: http://phpjs.org/functions/chunk_split    // +   original by: Paulo Ricardo F. Santos
    // +      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);
}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Paulo Ricardo F. Santos
1 Dec '08 Permalink

q  @ Kevin: No problem. :)

Gravatar
Kevin van Zonneveld
1 Dec '08 Permalink

q  @ Paulo Ricardo F. Santos: Ignore that. It works beautifully ;)

Gravatar
Kevin van Zonneveld
1 Dec '08 Permalink

q  @ Paulo Ricardo F. Santos: I added two hello world examples and ran them first in PHP. It seems the output strays a little bit from our current implementation: php adds a suffixing '*'.


Contribute a New function

Download

Download

There is a wide variety of packages if the default doesn't suit you.
You can also compile your own package to avoid any overhead.

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !

RSS

Tweets

Comments

Who uses php.js

If you use php.js, let us know and get linked.

Progress

php.js is complete for 83.7%

php.js on

Discuss php.js' future at Google Groups
Help improve php.js on github



Powered by php.js
Stats