JavaScript parse_url
Parse a URL and return its components
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 | function parse_url (str, component) { // Parse a URL and return its components // // version: 909.322 // discuss at: http://phpjs.org/functions/parse_url // + original by: Steven Levithan (http://blog.stevenlevithan.com) // + reimplemented by: Brett Zamir (http://brett-zamir.me) // % note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js // % note: blog post at http://blog.stevenlevithan.com/archives/parseuri // % note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js // % note: Does not replace invaild characters with '_' as in PHP, nor does it return false with // % note: a seriously malformed URL. // % note: Besides function name, is the same as parseUri besides the commented out portion // % note: and the additional section following, as well as our allowing an extra slash after // % note: the scheme/protocol (to allow file:/// as in PHP) // * example 1: parse_url('http://username:password@hostname/path?arg=value#anchor'); // * returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'} var o = { strictMode: false, key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], q: { name: "queryKey", parser: /(?:^|&)([^&=]*)=?([^&]*)/g }, parser: { strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-protocol to catch file:/// (should restrict this) } }; var m = o.parser[o.strictMode ? "strict" : "loose"].exec(str), uri = {}, i = 14; while (i--) {uri[o.key[i]] = m[i] || "";} // Uncomment the following to use the original more detailed (non-PHP) script /* uri[o.q.name] = {}; uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) { if ($1) uri[o.q.name][$1] = $2; }); return uri; */ switch (component) { case 'PHP_URL_SCHEME': return uri.protocol; case 'PHP_URL_HOST': return uri.host; case 'PHP_URL_PORT': return uri.port; case 'PHP_URL_USER': return uri.user; case 'PHP_URL_PASS': return uri.password; case 'PHP_URL_PATH': return uri.path; case 'PHP_URL_QUERY': return uri.query; case 'PHP_URL_FRAGMENT': return uri.anchor; default: var retArr = {}; if (uri.protocol !== '') {retArr.scheme=uri.protocol;} if (uri.host !== '') {retArr.host=uri.host;} if (uri.port !== '') {retArr.port=uri.port;} if (uri.user !== '') {retArr.user=uri.user;} if (uri.password !== '') {retArr.pass=uri.password;} if (uri.path !== '') {retArr.path=uri.path;} if (uri.query !== '') {retArr.query=uri.query;} if (uri.anchor !== '') {retArr.fragment=uri.anchor;} return retArr; } } |
Examples
Running
1 | parse_url('http://username:password@hostname/path?arg=value#anchor'); |
Should return
1 | {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'} |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have parse_url goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>