Parse URL (ActionScript 3.0)
I would like to know how would one parse an URL.
protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes
I need to get "this_is_what_i_want/even_if_it_has_slashes"
How should I do this?
Thanks!
Try this :
var u:String = 'protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes', a:Array = u.split('/'), s:String = '' for(var i=0; i<a.length; i++){ if(i > 3){ s += '/'+a[i] } } trace(s) // gives : /morethings/this_is_what_i_want/even_if_it_has_slashes
URLVariables, lastName + "," + urlVariables.firstName; // The second method uses the decode() method to parse the URL encoded string: var urlVariables:URLVariables = new� ActionScript ® 3.0 Reference for the Adobe ® Flash ® Platform Home | Show Packages and Classes List Hide Packages and Classes List | Packages | Classes | What's New | Index | Appendixes
Another approach would be using Regex like this:
.*?mydomain\.com[^\/]*\/[^\/]+\/[^\/]+\/([^?]*)
(Breakdown of the components.)
This looks for a pattern where it skips whatever comes before the domain name (doesn't matter if the protocol is specified or not), skips the domain name + TLD, skips any port number, and skips the first two sub path elements. It then selects whatever comes after it but skips any query strings.
Example: http://regexr.com/39r69
In your code, you could use it like this:
var url:String = "protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes"; var urlExp:RegExp = /.*?mydomain\.com[^\/]*\/[^\/]+\/[^\/]+\/([^?]*)/g; var urlPart:Array = urlExp.exec(url); if (urlPart.length > 1) { trace(urlPart[1]); // Prints "this_is_what_i_want/even_if_it_has_slashes" } else { // No matching part of the url found }
As you can see on the regexr link above, this captures the part "this_is_what_i_want/even_if_it_has_slashes" for all of these variations of the url:
protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes protocol://mydomain.com:8080/something/morethings/this_is_what_i_want/even_if_it_has_slashes protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes.html protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes.html?hello=world mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes protocol://subdomain.mydomain.com:8080/something/morethings/this_is_what_i_want/even_if_it_has_slashes
Edit: Fixed typo in regexp string
URLUtil, Dynamic parsing of url (uri) is often a part of scalable application. This tutorial demonstrates techniques of reading different parts of url String� AS3 Parse Class This is a static ActionScript 3.0 Class for interacting with the Parse REST API. It's primary usage is via an AIR application.
Simple way,
var file:String = 'protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes'; var splitted:Array = file.split('/'); var str1:String = splitted.splice(3).join('/'); //returns 'something/morethings/this_is_what_i_want/even_if_it_has_slashes' var str1:String = splitted.splice(5).join('/'); //returns 'this_is_what_i_want/even_if_it_has_slashes'
Parsing url string with regular expressions (RegExp) in ActionScript 3, Advanced ActionScript 3 with Design Patterns,2004, (isbn 0321426568, ean query string to the URL in the URLRequest object or using a URLVariables object. Next we'll revise the LimerickData class so that it loads the data and parses it� Podcast: We chat with Major League Hacking about all-nighters, cup stacking, and therapy dogs. Listen now.
If you want to be a little more flexible in the feature (e.g. you need the domain), you can use my Url class.
Class for URL parsing
package { import flash.net.URLVariables; public class Url { protected var protocol:String = ""; protected var domain:String = ""; protected var port:int = 0; protected var path:String = ""; protected var parameters:URLVariables; protected var bookmark:String = ""; public function Url(url:String) { this.init(url); } protected function splitSingle(value:String, c:String):Object { var temp:Object = {first: value, second: ""}; var pos:int = value.indexOf(c); if (pos > 0) { temp.first = value.substring(0, pos); temp.second = value.substring(pos + 1); } return temp; } protected function rtrim(value:String, c:String):String { while (value.substr(-1, 1) == c) { value = value.substr(0, -1); } return value; } protected function init(url:String):void { var o:Object; var urlExp:RegExp = /([a-z]+):\/\/(.+)/ var urlPart:Array = urlExp.exec(url); var temp:Array; var rest:String; if (urlPart.length <= 1) { throw new Error("invalid url"); } this.protocol = urlPart[1]; rest = urlPart[2]; o = this.splitSingle(rest, "#"); this.bookmark = o.second; rest = o.first; o = this.splitSingle(rest, "?"); o.second = this.rtrim(o.second, "&"); this.parameters = new URLVariables(); if (o.second != "") { try { this.parameters.decode(o.second); } catch (e:Error) { trace("Warning: cannot decode URL parameters. " + e.message + " " + o.second); } } rest = o.first o = this.splitSingle(rest, "/"); if (o.second != "") { this.path = "/" + o.second; } rest = o.first; o = this.splitSingle(rest, ":"); if (o.second != "") { this.port = parseInt(o.second); } else { switch (this.protocol) { case "https": this.port = 443; break; case "http": this.port = 80; break; case "ssh": this.port = 22; break; case "ftp": this.port = 21; break; default: this.port = 0; } } this.domain = o.first; } public function getDomain():String { return this.domain; } public function getProtocol():String { return this.protocol; } public function getPath():String { return this.path; } public function getPort():int { return this.port; } public function getBookmark():String { return this.bookmark; } public function getParameters():URLVariables { return this.parameters; } } }
Example usage
try { var myUrl:Url = new Url("protocol://mydomain.com/something/morethings/this_is_what_i_want/even_if_it_has_slashes"); trace("Protocol: " + myUrl.getProtocol()); trace("Domain: " + myUrl.getDomain()); trace("Path: " + myUrl.getPath()); trace("What you want: " + myUrl.getPath().split("/").splice(2).join("/") ); } catch (e:Error) { trace("Warning: cannot parse url"); }
Output
Protocol: protocol Domain: mydomain.com Path: /something/morethings/this_is_what_i_want/even_if_it_has_slashes What you want: morethings/this_is_what_i_want/even_if_it_has_slashes
Description
The init function checks with the regular expression if the given url starts with some letters (the protocol) followed by a colon, two slashes and more characters.
If the url contains a hash letter, everything behind its fist occurrence is taken as a bookmark
If the url contains a question mark, everything behind its fist occurrence is taken as key=value variables and parsed by the URLVariables class.
If the url contains a slash, everything behind its first occurrence is taken as the path
If the rest (everything between the last protocol slash and the first slash of the path) contains a colon, everything behind it will be converted to an integer and taken as the port. If the port is not set, a default will be set in dependency of the protocol
The rest is the domain
For answering your question, I use the path of the given url, split it by slash, cut of the 'something' and join it by slash.
Sending and Loading Variables, Below is a class that that can be used to send a group of variables to a php script on a server and receive the results. It was created by Rick� I have tested the code in action script 2.0 . it is working great but it does not support GIF so i want it write in action script 3.0. But i have no idea in this. var current_loader: Numb
Send/Receive Variables from Actionscript 3 & PHP (an OOP , The example is written in Flex 3 and ActionScript 3, although I have URLRequestMethod; private var loader:URLLoader; //url of rss 2.0 feed� ActionScript ® 3.0 Reference for the Adobe ® Flash ® Platform Home | Show Packages and Classes List Hide Packages and Classes List | Packages | Classes | What's New | Index | Appendixes
Parsing RSS 2.0 Feeds in ActionScript 3, A curated list of awesome libraries and components for ActionScript 3 and Linkify-as3 - Convert URLs, e-mail addresses, phone numbers, into clickable links. Es decir, nada útil. Ahora sí veremos cosas de Actionscript 3 que no sólo son nuevas y lindas sino realmente necesarias y cruciales para hacer lo que sea. Aún así has de leer la introducción a Actionscript 3.0 y descargar el Flash 9 Alpha o usen Flex Builder 2. Hacer un botón que llevara a una URL en Flash era algo sencillo.
robinrodricks/awesome-actionscript3: A curated list of , The URL that the HTTPService object should use when computing relative URLs. [static] The result format "object" specifies that the value returned is XML but is parsed as a tree of ActionScript objects. Language Version : ActionScript 3.0� i want to download this file: [URL] and the retrive the first 5 photos links to my flash application. View 1 Replies Actionscript 3 :: Unable To Parse JSON Using Flash Jun 1, 2011. I am attempting to parse some JSON from a URL via Flash/AS3. Here's my code so far: > import com.adobe.serialization.json.JSON;
Comments
- Hi - Can you add some context to the problem you're trying to solve? I.e. Is this the URL coming from the browser or input from a form? Do you only ALWAYS need the last two parameters in the URL path?
- Hello, it will come from the browser. I always need everything that comes after "something" (note that something will be dynamic)