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; } } |
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.
It's working for me...
explode('-+-', 'abc-+-def-+-ghi') // abc,def,ghi
What are you trying to do that's different?
PHP explode function allows you to split by a string, not only one character, which this JS function does not allow...
@valulgi: You'll need to look a bit closer. This function is really built around the split function from js.
@ d3x: Thanks! I've updated the function. I changed the
[CODE="Javascript"]
arguments.length != 3
[/CODE]
part, for it to allow 2 arguments. Other than that, great contribution!!
Same function with the limit attribute:
[CODE="Javascript"]
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]
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.
@ kenneth: That was pretty ugly indeed, thanks for contributing, I've updated the source and added your name.
This function is wrong. Params are reversed, and return values are not what they would be in PHP.
The "description" 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="Javascript"]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]


a
Jun 14th