http://www.webmasterworld.com Welcome to WebmasterWorld Guest from 203.130.245.155
register, login, search, glossary, subscribe, help, library, PubCon, recent posts, unanswered posts
SearchEngineWorld
Home / Forums Index / Browser Side World / JavaScript and AJAX
Forum Library : Charter : Moderator: open

JavaScript and AJAX

These terms have been highlighted:
explode in javascript [ remove highlighting ]

  
Javascript Equivalent of PHP's explode() and implode()
benj0323


#:1493248
 8:33 am on Feb. 25, 2005 (utc 0)

Basically I need to know the javascript's equivalent of php's explode and implode functions.

Here is what the explode and implode functions do:

Let's say I have a string:

$some_string = '13:42:43:56';

I want to break down each number into an array called $num_array. in PHP I could just do this:

$num_array = explode(':',$some_string);

The PHP implode function would produce the following:

$num_array[0] = 13
$num_array[1] = 42
$num_array[2] = 43
$num_array[3] = 56

Then let's say I wanted to piece it back into the original string. I could just do this in php:

$some_string = implode(':',$num_array);

Any idea how to accomplish this in javascript? Thanks a lot for your help and time. I really appreciate it.

benj0323


#:1493249
 8:42 am on Feb. 25, 2005 (utc 0)

I found this for exploding arrays:

function explodeArray(item,delimiter) {
tempArray=new Array(1);
var Count=0;
var tempString=new String(item);

while (tempString.indexOf(delimiter)>0) {
tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
Count=Count+1
}

tempArray[Count]=tempString;
return tempArray;
}

or you could do this

var myString = new String('red,green,blue');
var myArray = myString.split(',');

I still can't seem to figure out how to implode though.

SpaceFrog


#:1493250
 9:32 am on Feb. 25, 2005 (utc 0)

join for implode ...

myarray.join(';')
would join elements aof array into string with ; separator

Bernard Marx


#:1493251
 12:12 pm on Feb. 25, 2005 (utc 0)

var myString = new String('red,green,blue');
var myArray = myString.split(',');

There's no need to explicitly create a String object. This is done automatically.

var myArray = 'red,green,blue'.split(',');

SpaceFrog


#:1493252
 1:18 pm on Feb. 25, 2005 (utc 0)

I sometimes use this technique when I retrieve a string with separators form an input and want to convert it into an array ...

 

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of ServicePrivacy PolicyReport ProblemAbout
WebmasterWorld � and PubCon � are a Registered Trademarks of WebmasterWorld Inc.
WebmasterWorld Inc. / SearchEngineWorld 1996-2008 all rights reserved