PDA

View Full Version : explode() & implode()


LoneAngelIbesu
12-31-2008, 10:59 PM
Some PHP classics.

explode(delimiter, string, limit); - returns an array from a string, split by the given delimiter. Optionally, a limit can be supplied.
- delimiter: a boundary string
- string: the source string
- limit: if limit is set, the returned array will contain a maximum number of limit elements, with remaining part of string attached to the last element. If limit is negative, all but the last limit elements are returned.

Note that if delimiter is set to an empty string ("") or 0, explode() will return FALSE.


function explode(delimiter, string, limit) {
temp.tokens = temp.string.tokenize(temp.delimiter);
if(!temp.delimiter || temp.delimiter == "") {
return false;
}
if(!temp.limit || temp.limit == 0) {
return temp.string.tokenize(temp.delimiter);
}
if(temp.limit > temp.tokens.size()) {
temp.limit = temp.tokens.size();
}
else if(temp.limit > 0) {
for(temp.i = 0; temp.i <= temp.limit - 1; temp.i ++) {
temp.output.add(temp.tokens[temp.i]);
}
for(temp.i = 0; temp.i < temp.tokens.size() - temp.limit; temp.i ++) {
temp.output[temp.limit - 1] @= temp.delimiter @ temp.tokens[temp.limit + temp.i];
}
}
else {
temp.limit *= -1;
for(temp.i = 0; temp.i <= temp.limit - 1; temp.i ++) {
temp.output.add(temp.tokens[temp.i]);
}
}
return temp.output;
}

Example:
function onCreated() {
temp.string = "this!is!my!string";
temp.i = explode("!", temp.string);
temp.i2 = explode("!", temp.string, 2);
temp.i3 = explode("!", temp.string, -2);
echo("Starting string: " @ temp.string);
echo("Exploded: " @ temp.i @ "; " @ temp.i2 @ "; " @ temp.i3);
}

Returns:
Starting string: this!is!my!string
Exploded: this,is,my,string; this,is!my!string; this,is

------------------------------------------------------------------------

implode(glue, pieces); - joins array elements with an optional glue string.
- glue: the separating string
- pieces: an array


function implode(glue, pieces) {
for(temp.i = 0; temp.i < temp.pieces.size(); temp.i ++) {
if(temp.glue) {
if(temp.i != temp.pieces.size() - 1) {
temp.output @= temp.pieces[temp.i] @ temp.glue;
}
else {
temp.output @= temp.pieces[temp.i];
}
}
else {
temp.output @= temp.pieces[temp.i];
}
}
return temp.output;
}


Example:
function onCreated() {
temp.pieces = {
"this", "is", "my", "array"
};
temp.i = implode(null, temp.pieces);
temp.i2 = implode(",", temp.pieces);
temp.i3 = implode("#", temp.pieces);
temp.i4 = implode("-+-", temp.pieces);
echo("Starting array: " @ temp.pieces);
echo("Imploded: " @ temp.i @ "; " @ temp.i2 @ "; " @ temp.i3 @ "; " @ temp.i4);
}

Returns:
Starting array: this,is,my,array
Imploded: thisismyarray; this,is,my,array; this#is#my#array; this-+-is-+-my-+-array

cbk1994
01-01-2009, 02:00 AM
Very nice.

Codein
01-01-2009, 02:38 PM
Nice work :D

LoneAngelIbesu
01-09-2010, 06:46 PM
I have an update... a year later. There is something wrong with the explode() function currently in the first post. I can't remember exactly what it is, but the output is incorrect for certain situations. The following version should fix those problems:

public function explode(delimiter, string, limit) {
temp.tokens = temp.string.tokenize(temp.delimiter);
if(!temp.delimiter || temp.delimiter == "") {
return false;
}
if(!temp.limit || temp.limit == 0) {
return temp.string.tokenize(temp.delimiter);
}
if(temp.limit > temp.tokens.size()) {
temp.limit = temp.tokens.size();
}

if(temp.limit > 0) {
for(temp.i = 0; temp.i <= temp.limit - 1; temp.i ++) {
temp.output.add(temp.tokens[temp.i]);
}
for(temp.i = 0; temp.i < temp.tokens.size() - temp.limit; temp.i ++) {
temp.output[temp.limit - 1] @= temp.delimiter @ temp.tokens[temp.limit + temp.i];
}
}
else {
temp.limit *= -1;
for(temp.i = 0; temp.i <= temp.limit - 1; temp.i ++) {
temp.output.add(temp.tokens[temp.i]);
}
}
return temp.output;
}