Javascript Explode
Friday, April 2, 2010 0:32Posted in category JavaScript
No Comments
What is the equivalent function for php explode in Javascript?
There is no explode function in java script.but we can handle the explode functionality by writing some custom functions. here is a simple java script code for you.
If you Run the following code
explode(‘. ‘, ‘blog.altmint.com);
It Could return
1.{0: ‘blog’, 1: ‘altmint’, 2: ‘com’}
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: 810.114
// 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;
}
}
Click here to Read more.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.