UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

98 lines (86 loc) 4.24 kB
--- layout: page title: "JavaScript parse_url function" comments: true sharing: true footer: true alias: - /functions/view/parse_url:485 - /functions/view/parse_url - /functions/view/485 - /functions/parse_url:485 - /functions/485 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's parse_url {% codeblock url/parse_url.js lang:js https://raw.github.com/kvz/phpjs/master/functions/url/parse_url.js raw on github %} function parse_url (str, component) { // From: http://phpjs.org/functions // + 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 query, 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] = {}; query = uri[key[12]] || ''; query.replace(parser, function ($0, $1, $2) { if ($1) {uri[name][$1] = $2;} }); } delete uri.source; return uri; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/url/parse_url.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/url/parse_url.js) ### Example 1 This code {% codeblock lang:js example %} parse_url('http://username:password@hostname/path?arg=value#anchor'); {% endcodeblock %} Should return {% codeblock lang:js returns %} {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'} {% endcodeblock %} ### Other PHP functions in the url extension {% render_partial _includes/custom/url.html %}