Explode() Function

Those of you who have used PHP, which is a server side language script, know exactly how useful explode() function is. In PHP, it creates arrays from a string in which each element of the array is separated by a sign you specify—for example, a semicolon. Well, but it's nowhere to be seen in Flash!


So, while working on a project that required to explode() strings really badly, I found Flash to lack this particular function. "What should I do?," I asked myself. The answer to that question arrived pretty fast: You must find a function that will do the job. Shouldn't be too hard, provided that I couldn't have been the only one who needed the explode function.

Here's what I found:

function explode(separator:String, string:String) {
var list = new Array();
if (separator == null) return false;
if (string == null) return false;
var currentStringPosition = 0;
while (currentStringPosition<string.length) {
var nextIndex = string.indexOf(separator, currentStringPosition);
if (nextIndex == -1) break;
var word = string.slice(currentStringPosition, nextIndex);
list.push(word);
currentStringPosition = nextIndex+1;
}
if (list.length<1) {
list.push(string);
} else {
list.push(string.slice(currentStringPosition, string.length));
}
return list;
}

 

This function works in exactly the same way as PHP explode() does. So, for example:

mystring="element1;element2;element3"; // some string
newstring=explode(";",mystring); //using explode()
trace(newstring); //output:element1, element2, element3

More Advance Implementation of explode()

Viola!  We've got them all in an array. But... Hold on, why would we need an array? To find out we will now create a small program that loads external data, and then parses it using explode().

Shall we begin?

Step 1
Create four textfield with the Text Tool in your Tools menu. Give each of them an instance name, respectively: field0, field1, field2, field3.

[the four textfields]

Step 2
In the same directory as your fla, set up a textfield and include the following:
names=John;Mark;Jane;Mike. Save the files as data.txt.

Step 3

Select the first frame of the main Timeline and copy and paste this code:

function explode(separator:String, string:String) {
var list = new Array();
if (separator == null) return false;
if (string == null) return false;
var currentStringPosition = 0;
while (currentStringPosition<string.length) {
var nextIndex = string.indexOf(separator, currentStringPosition);
if (nextIndex == -1) break;
var word = string.slice(currentStringPosition, nextIndex);
list.push(word);
currentStringPosition = nextIndex+1;
}
if (list.length<1) {
list.push(string);
} else {
list.push(string.slice(currentStringPosition, string.length));
}
return list;
}
this.loadVariables("data.txt");
this.onData = function() {
if (names) {
array2 = explode(";",names);
for(i=0;i<=array2.length;i++){
_root["field" + i].text = array2[i];
}
}
};
stop();

 


Explanation 

this.loadVariables("data.txt");

This code is used to load external variables from text files (not only the ones with .txt extension - also PHP, ASP files).

Remember about including this, unless you want to put it directly into a textfield, and in such case your code will have to contain another two parameters: target and method, and it might look like this:

loadVariables("http://www.lashf.com/textfield.txt", "textfield", "GET");

But we don't need do that, so let's move on.

this.onData = function() {

We create a function that basically executes the code inside the curly brackets when data has completely downloaded from a file. It's useful because, after all, doing anything earlier than that doesn't make sense.

if (names) {

The if condition check if names is defined, and if it is, it proceeds to the code below. Keep in mind that the first line of the code is equivalent to: if (names !="undefined").

array2 = explode(";",names);

This code uses the function we defined earlier, i.e. saves an array from a string names to the variable array2.

for(i=0;i<=array2.length;i++){

We create a for loop, whose number of passes is equal to the number of elements in the array array2 (= four).

_root["field" + i].text = array2[i];

Finally, each of the text field is assigned the respective value.


OK, we are done with creating our simple program. I hope you enjoyed reading this tutorial as much as I did writing it.

As always, there's a FLA file below.

Cheers!

 

Average: 4.2 (15 votes)
FLA
Download FLA - Explode