PDA

View Full Version : string from flash to php and going for database


madthijs
03-23-2004, 03:15 PM
I have this string coming from my flash movie which is dynamically created. It can be many times longer than this, but it always has this lay-out :


woonboot1|210|148.75@ ufo1|210|73.5@ tractor1|100|100.3@


You can see 3 objects with a "|" splitting it. And the "@" marks a new input.


first i want to split the original string into pieces by cutting the string at the "@".
You now get an array like this :

array[0]=woonboot1|210|148.75
array[1]=ufo1|210|73.5
array[2]=tractor1|100|100.3
etc.

Secondly i want to split this array at the "|" so that i can save the info to my database. Keep in mind that you don´t know how long the original string is, so i think this must be done with a for loop ??


But how


Hope that i was a clear about it and get some help from you folks.

thnx,

Madthijs

splict
03-23-2004, 03:50 PM
As crazy as it sounds, the function to split a string up is: split(). ;) Something along these lines should work.

-splict
myString = "woonboot1|210|148.75@ ufo1|210|73.5@ tractor1|100|100.3@";
var tmp_arr = myString.split("@");
variables_arr = new Array();
for (var i = 0; i < tmp_arr.length; i++) {
variables_arr[i] = tmp_arr[i].split("|");
}

madthijs
03-23-2004, 03:56 PM
Hey splict, thanks for your help!


i knew about the split function :p also explode is usefull there 2.
but i couldn´t make the last part work.

I will try your code because i want to understand what i did wrong there, although i just found another solution to my ´problem´. A friend gave me this working script :


$tekst = $stuff; //$stuff is my original string

$box = explode("@", $tekst);

while( list($key, $val) = each($box) ) {

$item[$key] = explode("|", $val);
}

while( list($key, $val) = each($item[0]) ) {
echo $val. "<br>";
}

echo ($item[1][2]);


Still thanks for the help mate, and i will go and learn from your code now ;)

Madthijs

splict
03-23-2004, 04:05 PM
actually I misunderstood. I thought you were going to flash. The script your friend had is in php (which is what you wanted). If you like I will rewrite the code in php - its the same thing just different syntax. If you wanna post your non working code, I'll give it a look, too.

madthijs
03-23-2004, 04:09 PM
yeah i like 2 know what i did wrong here ....

I will post my script in a sec. Have to write it again, and must get the same errors :D

madthijs

madthijs
03-23-2004, 04:30 PM
hmmm when i created my script in the hope of finding the exact same errors i stumbled onto the solution to my problem :p


my code was OK . it was like this :

$arr_probeer = explode ("@", $stuff);
$result = count($arr_probeer)-1;

for ($i=0; $i <= $result; $i++) {

$arr_maken[$i] = explode ("|", $arr_probeer[$i]);
}


The problem was that i echo-ed wrong :

echo ($arr_maken[1]);

It had to be like this :

echo ($arr_maken[1][0]);



Thanks for the help splict!

If you have any suggestions or comments left, feel free to comment.


Madthijs

splict
03-23-2004, 04:56 PM
yeah thats pretty much what I had. One thing I would note is that you don't need the trailing @ sign. If you don't have control over the string outputted from flash you could just use substr() to remove it in php. The second line of code in the following removes the @ sign from both the beginning and the end of the string if they exist. This could be done (probably more efficiently) without a regex, I just used one to keep the code simple and on one line. If you don't get rid of the @ sign, though, your array will be the wrong length. You can see this demonstrated by commenting out the second line. Glad you got everything sorted. :)

-splict
<?php
$myString = '@woonboot1|210|148.75@ ufo1|210|73.5@ tractor1|100|100.3@';
$myString = preg_replace(array("/^@/", "/@$/"), "", $myString);
$tmp_arr = explode('@', $myString);
for ($i = 0; $i < count($tmp_arr); $i++) {
$variables_arr[$i] = explode('|', $tmp_arr[$i]);
}
// show output:
for ($i = 0; $i < count($variables_arr); $i++) {
echo "<br /><b>Set $i:</b> ";
for ($j = 0; $j < count($variables_arr[$i]); $j++) {
echo ' &nbsp; <b>'.$j.'.</b> '.$variables_arr[$i][$j];
}
}
?>

madthijs
03-23-2004, 05:02 PM
yeah i noticed that my array wasn´t the right length. I also saw that when writing to my database he created 1 blank entry. This is due to the fact that the count isn´t right.

Thanks again (i keep saying the same things ;) )

Madthijs