Use PHP functions in JavaScript

JavaScript explode

Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
function explode (delimiter, string, limit) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/explode    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
     var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' ) {
        return null;
    }
 
    if ( delimiter === '' ||        delimiter === false ||
        delimiter === null ) {
        return false;
    }
     if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' ) {
        return emptyArray;    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;    }
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
explode(' ', 'Kevin van Zonneveld');

Should return

1
{0: 'Kevin', 1: 'van', 2: 'Zonneveld'}

» Example 2

Running

1
explode('=', 'a=bc=d', 2);

Should return

1
['a', 'bc=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 explode 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
a
Jun 14th Permalink

q  ser

Gravatar
IVI-R3za.M
Feb 27th Permalink

q  its so great! thanks

Gravatar
Brett Zamir
Feb 15th Permalink

q  It's working for me...

explode('-+-', 'abc-+-def-+-ghi') // abc,def,ghi



What are you trying to do that's different?

Gravatar
nowotny
Feb 14th Permalink

q  PHP explode function allows you to split by a string, not only one character, which this JS function does not allow...

Gravatar
Onno Marsman
9 Dec '08 Permalink

q  @valulgi: You'll need to look a bit closer. This function is really built around the split function from js.

Gravatar
valulgi
8 Dec '08 Permalink

q  why not use the split function from js?

Gravatar
Kevin van Zonneveld
17 May '08 Permalink

q  @ d3x: Thanks! I've updated the function. I changed the
[CODE=&quot;Javascript&quot;]
arguments.length != 3
[/CODE]
part, for it to allow 2 arguments. Other than that, great contribution!!

Gravatar
d3x
17 May '08 Permalink

q  Same function with the limit attribute:

[CODE=&quot;Javascript&quot;]
function explode( delimiter, string, limit ) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: kenneth
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: explode(' ', 'Kevin van Zonneveld');
// * returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
// + further improved by: d3x
// * example 1: explode('=', 'a=bc=d', 2);
// * returns : ['a', 'bc=d']

var emptyArray = { 0: '' };

if ( arguments.length != 3
|| typeof arguments[0] == 'undefined'
|| typeof arguments[1] == 'undefined'
|| typeof arguments[2] == 'undefined' )
{
return null;
}

if ( delimiter === ''
|| delimiter === false
|| delimiter === null )
{
return false;
}

if ( typeof delimiter == 'function'
|| typeof delimiter == 'object'
|| typeof string == 'function'
|| typeof string == 'object' )
{
return emptyArray;
}

if ( delimiter === true ) {
delimiter = '1';
}
if(!limit){
return string.toString().split ( delimiter.toString() );
} else {
var splitted = string.toString().split(delimiter.toString());
var partA = splitted.splice(0, limit - 1);
var partB = splitted.join(delimiter.toString());
partA.push(partB);
return partA;
}
}
[/CODE]

Gravatar
u24
6 Feb '08 Permalink

q  great project.

this one doesn't replicate the optional third $limit parameter which php has though. If I get a spare second I'll add another comment with amended code.

Gravatar
Kevin van Zonneveld
1 Feb '08 Permalink

q  @ kenneth: That was pretty ugly indeed, thanks for contributing, I've updated the source and added your name.

Gravatar
kenneth
1 Feb '08 Permalink

q  This function is wrong. Params are reversed, and return values are not what they would be in PHP.

The &quot;description&quot; above is actually correct, but both the actual source code given and the example given have the params reversed.

As for return values....I might as well just right it out at this point:

[CODE=&quot;Javascript&quot;]function explode( /* delimiter, string */ ) {

var emptyArray = { 0: '' };

if ( arguments.length != 2
|| typeof arguments[0] == 'undefined'
|| typeof arguments[1] == 'undefined' )
{
return null;
}

var delimiter = arguments[0];
var string = arguments[1];

if ( delimiter === ''
|| delimiter === false
|| delimiter === null )
{
return false;
}

if ( typeof delimiter == 'function'
|| typeof delimiter == 'object'
|| typeof string == 'function'
|| typeof string == 'object' )
{
return emptyArray;
}

if ( delimiter === true ) {
delimiter = '1';
}

return string.toString().split ( delimiter.toString() );

}[/CODE]


Contribute a New function