views:

93

answers:

3

HI

I have a URL

var str:String = "conn=rtmp://server.com/service/&fileId=myfile.flv"

or

var str:String = "fileId=myfile.flv&conn=rtmp://server.com/service/"

The str might be like this, But i need to get the value of "conn" and "fileId" from the string.

how can i write a function for that.

A: 
 var url:String = "fileId=myfile.flv&conn=rtmp://server.com/service/";

 var strArray:Array = url.split(/=/);

trace(strArray[0]) //Just to test

returns an array, with the word 'conn or fileid' in index 0 - 2 (anything even), alternatives of 1, 3 is the information within.


Or was it something else you needed?

Glycerine
M28
+1  A: 
var str:String = "fileId=myfile.flv&conn=rtmp://server.com/service/"
var fa:Array = str.split("&");
for(var i:uint=0;i<fa.length;i++)
    fa[i] = fa[i].split('=');

That's how the "fa" variable be in the end:

fa = [
["fileId","myfile.flv"],
["conn","rtmp://server.com/service/"]
]

M28
+1  A: 

I'm guessing that you're having trouble with the second '=' in the string. Fortunatly, ActionScript's String.Split method supports splitting on strings, so the following code should work:

var str:String = "conn=rtmp://server.com/service/&fileId=myfile.flv";
var conn:String = (str + "&").Split("conn=")[1].Split("&")[0];

and

var str:String = "fileId=myfile.flv&conn=rtmp://server.com/service/";
var fileId:String = (str + "&").Split("fileId=")[1].Split("&")[0];

Note: I'm appending a & to the string, in case the string didn't contain any url parameters beyond the one we're looking for.

Prutswonder
Thank you this is working
coderex
Yes, you did. But it contains a for-loop, doesn't handle single-parameter URLs and has no explanation. Not very nice of you to downgrade a more complete answer. :-\
Prutswonder
My answer can handle any number of arguments, it won't lag because it have a simple for loop -__-
M28