Tag Archives: explode

JavaScript: How to Explode a String into an Array Programming 13 FEB 2012

Coming from PHP, I’m well versed in using the handy explode function to force a string into an array, using a specified delimiter character to chop up the string into the little bits that are to be stored in the array.

Happily for me, plain old vanilla JavaScript also has this ability built into the language, choosing to call its version of this functionality “split”.

From the official definition: The split() method is used to split a string into an array of substrings, returning the new array containing the substrings.

It accepts two parameters, namely the separator and the limit, both of which are actually optional by the way. The separator parameter is where you specify the delimiter character (if you omit this, the entire string will be stuffed into a single element array ). The optional limit parameter if set will control the number of splits.

A few examples on using split:

var str="How are you doing today?";

//some split usage examples
document.write(str.split() + "<br />");
document.write(str.split(" ") + "<br />");
document.write(str.split("") + "<br />");
document.write(str.split(" ",3));

//the output (',' indicates array boundary)
//How are you doing today?
//How,are,you,doing,today?
//H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
//How,are,you 

Useful.

PHP: Simple Method to Implode or Explode Arrays with Keys CodeUnit 21 NOV 2011

Associative arrays are very useful beasts, because values tied to keys makes it so much easier to build up pseudo objects in that it makes it easier to see which piece of array data refers to what.

There are a number of ways to store associative arrays of course, but this particular one below is probably one of the simplest for getting a single string out of a one level array with keys, and then transforming back into its array form when you need to make use of it again.

function array_implode_with_keys($array){
    $return = '';
    if (count($array) > 0){
    foreach ($array as $key=>$value){
    $return .= $key . '||||' . $value . '----';
    }
    $return = substr($return,0,strlen($return) - 4);
    }
    return $return;
}

function array_explode_with_keys($string){
    $return = array();
    $pieces = explode('----',$string);
    foreach($pieces as $piece){
        $keyval = explode('||||',$piece);
        if (count($keyval) > 1){
        $return[$keyval[0]] = $keyval[1];
        } else {
        $return[$keyval[0]] = '';
        }
    }
    return $return;
}

If you look at the code above, you will see that we are making use of delimiters here to keep firstly the keys aparts, and secondly the key from its associated value. In this case we are using —- to split the array elements, and |||| to split the key from its value.

In other words the resulting string is from the implode function is key1||||value1—-key2||||value2—-key3||||value3.

Obviously the explode function just works in reverse.

A Shocking Way to Start One’s Morning My Life 26 JUN 2010

Shame, I must say I’m quite glad that it wasn’t me who had such a disastrous start to the morning as poor old C this Friday!

Definitely not a shot of Chantelle as she looked yesterday morning! :P

It all started off on the wrong foot when she woke up crying thanks to a bad dream she had involving leaving me for Altus of 7de Laan (don’t ask – anything she sees or hears on TV the night before appears to be creeping into her dreams at the moment, making for some pretty funny conversations in the morning!).

A simple big hug sorted that one out, but unfortunately as we all know, things go wrong in threes and so while I was happily munching away on my muesli, Chantelle waltzed in clutching the broken frame of her spectacles on which she had just sat!

And to top it all off, as I pulled up in the parking lot after my morning commute, I got a phone call informing me that her hair dryer had literally just blown up in her hand, taking all the plugs with it.

Needless to say, I immediately instructed her to drop everything and get back into bed, lest she destroy anything else during the course of the day! ;)