Use PHP functions in JavaScript

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
function parse_url (str, component) {
    // Parse a URL and return its components  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/parse_url    // +      original by: Steven Levithan (http://blog.stevenlevithan.com)
    // + reimplemented by: Brett Zamir (http://brett-zamir.me)
    // + input by: Lorenzo Pisani
    // + input by: Tony
    // + improved 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 invalid characters with '_' as in PHP, nor does it return false with
    // %          note: a seriously malformed URL.    // %          note: Besides function name, is essentially the same as parseUri as well as our allowing
    // %          note: an extra slash after 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 key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port',                         'relative', 'path', 'directory', 'file', 'query', 'fragment'],
        ini = (this.php_js && this.php_js.ini) || {},
        mode = (ini['phpjs.parse_url.mode'] && 
            ini['phpjs.parse_url.mode'].local_value) || 'php',
        parser = {            php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
            strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
            loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this)
        };
     var m = parser[mode].exec(str),
        uri = {},
        i = 14;
    while (i--) {
        if (m[i]) {          uri[key[i]] = m[i];  
        }
    }
 
    if (component) {        return uri[component.replace('PHP_URL_', '').toLowerCase()];
    }
    if (mode !== 'php') {
        var name = (ini['phpjs.parse_url.queryKey'] && 
                ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey';        parser = /(?:^|&)([^&=]*)=?([^&]*)/g;
        uri[name] = {};
        uri[key[12]].replace(parser, function ($0, $1, $2) {
            if ($1) {uri[name][$1] = $2;}
        });    }
    delete uri.source;
    return uri;
}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Patrick
May 11th Permalink

q  Hey guys! parse url saved my day. That easy, that great. I will include phpjs in my projects <3 thank you

Gravatar
بوابة نعم
Mar 22nd Permalink

q  New and exclusive news on the gate Yes

Gravatar
Brett Zamir
27 Mar '11 Permalink

q  @Aaron. Can you provide more details? I tried on Apache, and it works fine. Or did you mean you tried in SSJS? What regex and sample are you using? One thing you can try is replacing the various "()" marks used in the php mode regex with the equivalent but longer, "(.{0})". Maybe Firefox4's new regex parser doesn't properly handle "()" under some conditions. If that doesn't work, let me know if changing by default to "strict" or "loose" mode instead of "php" (see the code on line 25 with "|| 'php'").

Gravatar
Aaron
25 Mar '11 Permalink

q  I've been using this script for two years. I just switched to FF4 and it now fails. Error: "regular expression too complex" fails here in firebug "var m = parser[mode].exec(str)," only on a web server. Works fine locally.

Gravatar
Brett Zamir
12 Mar '11 Permalink

q  @Tony & @Lorenzo Pisani: Also, I should mention, the fix is available in Git (raw js source URL).

Gravatar
Brett Zamir
12 Mar '11 Permalink

q  @radekk: Can you clarify?
@Tony & @Lorenzo Pisani: Finally getting to this. The issue was simply that the loose mode had been chosen by default. This should probably work more like PHP now, and I also cleaned up the function a bit and allowed custom ini settings to change the parsing mode (e.g., "loose" mode (set by "phpjs.parse_url.mode") is more useful when trying to guess at a user's imperfect input, but faulty as you found out; "strict" follows the same as the default "php" mode, but offers more properties, including parsing the query string further).

Gravatar
Tony
11 Mar '11 Permalink

q  This fails with a simple code like this :

var url = "page.html"

it shows that host is page.html

Gravatar
radekk
19 Oct '10 Permalink

q  You didn't mentioned that it's doesn't work correctly when you use variables like (in GET):

- scheme
- host
- user
- path
... and so on.

I think that parameters should be separated and enclosed in an second object to not collide with each other. That's my opinion.

Gravatar
Lorenzo Pisani
30 Mar '10 Permalink

q  this fails on some urls

in PHP I can do this:

parse_url('hello/world', PHP_URL_PATH) // gives me back hello/world



but I simply get /world back from php.js because it thinks hello is the host


Contribute a New function

More functions

In this category

base64_decode
base64_encode
get_headers
get_meta_tags
http_build_query
» parse_url
rawurldecode
rawurlencode
urldecode
urlencode

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !