Created
August 27, 2015 07:54
-
-
Save butuzov/80d82e341f9273213255 to your computer and use it in GitHub Desktop.
URI Parsing with Javascript (alternative)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_url(url) { | |
/* Dom parsing of A | |
based at https://gist.github.com/jlong/2428561 | |
*/ | |
var parser = document.createElement('a'); | |
parser.href = url; | |
/* Exception for user:password */ | |
var re = /(\/\/)(.+?)((:(.+?))?)@/i; | |
if ( re.exec(url) ){ | |
var result = re.exec(url) | |
if (typeof result[2] == "string" && result[2].length > 0) | |
parser.user = result[2]; | |
if (typeof result[5] == "string" && result[5].length > 0) | |
parser.pass = result[5]; | |
} | |
var urls = new Object(); | |
var urls_php = ['scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment']; | |
var urls_js = ['protocol', 'hostname', 'port', 'user', 'pass', 'pathname', 'search', 'hash']; | |
urls_js.forEach(function(value, index){ | |
if (typeof parser[value] == "string" && parser[value].length > 0) | |
switch(value){ | |
case "protocol": | |
urls[ urls_php[index] ] = parser[value].replace(":", ""); | |
break; | |
case "hash": | |
urls[ urls_php[index] ] = parser[value].replace("#", ""); | |
break; | |
case "pathname": | |
if (parser[value] != "/") | |
urls[ urls_php[index] ] = parser[value]; | |
break; | |
default: | |
urls[ urls_php[index] ] = parser[value]; | |
break; | |
} | |
}); | |
return urls; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*********************************************** | |
* This is a test-case for uri.js | |
************************************************/ | |
/* | |
scheme - e.g. http | |
host | |
port | |
user | |
pass | |
path | |
query - after the question mark ? | |
fragment - after the hashmark # | |
*/ | |
$urls = array( | |
'http://oleg:qwert@example.com:81/demo/file.php?foo=bar#123', | |
'ftp://oleg:qwert@example.com/demo/', | |
'https://oleg@example.com/demo/', | |
'http://yahoo.com', | |
'http://example.com:443/demo/', | |
); | |
?> | |
<script> | |
function var_export(mixed_expression, bool_return) { | |
// discuss at: http://phpjs.org/functions/var_export/ | |
// original by: Philip Peterson | |
// improved by: johnrembo | |
// improved by: Brett Zamir (http://brett-zamir.me) | |
// input by: Brian Tafoya (http://www.premasolutions.com/) | |
// input by: Hans Henrik (http://hanshenrik.tk/) | |
// bugfixed by: Brett Zamir (http://brett-zamir.me) | |
// bugfixed by: Brett Zamir (http://brett-zamir.me) | |
// depends on: echo | |
// example 1: var_export(null); | |
// returns 1: null | |
// example 2: var_export({0: 'Kevin', 1: 'van', 2: 'Zonneveld'}, true); | |
// returns 2: "array (\n 0 => 'Kevin',\n 1 => 'van',\n 2 => 'Zonneveld'\n)" | |
// example 3: data = 'Kevin'; | |
// example 3: var_export(data, true); | |
// returns 3: "'Kevin'" | |
var retstr = '', | |
iret = '', | |
value, | |
cnt = 0, | |
x = [], | |
i = 0, | |
funcParts = [], | |
// We use the last argument (not part of PHP) to pass in | |
// our indentation level | |
idtLevel = arguments[2] || 2, | |
innerIndent = '', | |
outerIndent = '', | |
getFuncName = function(fn) { | |
var name = (/\W*function\s+([\w\$]+)\s*\(/) | |
.exec(fn); | |
if (!name) { | |
return '(Anonymous)'; | |
} | |
return name[1]; | |
}; | |
_makeIndent = function(idtLevel) { | |
return (new Array(idtLevel + 1)) | |
.join(' '); | |
}; | |
__getType = function(inp) { | |
var i = 0, | |
match, types, cons, type = typeof inp; | |
if (type === 'object' && (inp && inp.constructor) && | |
getFuncName(inp.constructor) === 'PHPJS_Resource') { | |
return 'resource'; | |
} | |
if (type === 'function') { | |
return 'function'; | |
} | |
if (type === 'object' && !inp) { | |
return 'null'; // Should this be just null? | |
} | |
if (type === 'object') { | |
if (!inp.constructor) { | |
return 'object'; | |
} | |
cons = inp.constructor.toString(); | |
match = cons.match(/(\w+)\(/); | |
if (match) { | |
cons = match[1].toLowerCase(); | |
} | |
types = ['boolean', 'number', 'string', 'array']; | |
for (i = 0; i < types.length; i++) { | |
if (cons === types[i]) { | |
type = types[i]; | |
break; | |
} | |
} | |
} | |
return type; | |
}; | |
type = __getType(mixed_expression); | |
if (type === null) { | |
retstr = 'NULL'; | |
} else if (type === 'array' || type === 'object') { | |
outerIndent = _makeIndent(idtLevel - 2); | |
innerIndent = _makeIndent(idtLevel); | |
for (i in mixed_expression) { | |
value = this.var_export(mixed_expression[i], 1, idtLevel + 2); | |
value = typeof value === 'string' ? value.replace(/</g, '<') | |
. | |
replace(/>/g, '>') : value; | |
x[cnt++] = innerIndent + i + ' => ' + | |
(__getType(mixed_expression[i]) === 'array' ? | |
'\n' : '') + value; | |
} | |
iret = x.join(',\n'); | |
retstr = outerIndent + 'array (\n' + iret + '\n' + outerIndent + ')'; | |
} else if (type === 'function') { | |
funcParts = mixed_expression.toString() | |
. | |
match(/function .*?\((.*?)\) \{([\s\S]*)\}/); | |
// For lambda functions, var_export() outputs such as the following: | |
// '\000lambda_1'. Since it will probably not be a common use to | |
// expect this (unhelpful) form, we'll use another PHP-exportable | |
// construct, create_function() (though dollar signs must be on the | |
// variables in JavaScript); if using instead in JavaScript and you | |
// are using the namespaced version, note that create_function() will | |
// not be available as a global | |
retstr = "create_function ('" + funcParts[1] + "', '" + | |
funcParts[2].replace(new RegExp("'", 'g'), "\\'") + "')"; | |
} else if (type === 'resource') { | |
retstr = 'NULL'; // Resources treated as null for var_export | |
} else { | |
retstr = typeof mixed_expression !== 'string' ? mixed_expression : | |
"'" + mixed_expression.replace(/(["'])/g, '\\$1') | |
. | |
replace(/\0/g, '\\0') + "'"; | |
} | |
if (!bool_return) { | |
document.write(retstr); | |
return null; | |
} | |
return retstr; | |
} | |
</script> | |
<script> | |
function parse_url(url) { | |
/* Dom parsing of A */ | |
var parser = document.createElement('a'); | |
parser.href = url; | |
/* Exception for user:password */ | |
var re = /(\/\/)(.+?)((:(.+?))?)@/i; | |
if ( re.exec(url) ){ | |
var result = re.exec(url) | |
if (typeof result[2] == "string" && result[2].length > 0) | |
parser.user = result[2]; | |
if (typeof result[5] == "string" && result[5].length > 0) | |
parser.pass = result[5]; | |
} | |
var urls = new Object(); | |
var urls_php = ['scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment']; | |
var urls_js = ['protocol', 'hostname', 'port', 'user', 'pass', 'pathname', 'search', 'hash']; | |
urls_js.forEach(function(value, index){ | |
if (typeof parser[value] == "string" && parser[value].length > 0) | |
switch(value){ | |
case "protocol": | |
urls[ urls_php[index] ] = parser[value].replace(":", ""); | |
break; | |
case "hash": | |
urls[ urls_php[index] ] = parser[value].replace("#", ""); | |
break; | |
case "pathname": | |
if (parser[value] != "/") | |
urls[ urls_php[index] ] = parser[value]; | |
break; | |
default: | |
urls[ urls_php[index] ] = parser[value]; | |
break; | |
} | |
}); | |
return urls; | |
} | |
</script> | |
<?php foreach($urls as $url) { ?> | |
<hr> | |
<?php var_dump($url); ?> | |
<hr> | |
<h6 style="margin:0;">PHP</h6> | |
<pre><?php var_export( count(parse_url($url)) ); ?></pre> | |
<pre><?php var_export( parse_url($url) ); ?></pre> | |
<h6 style="margin:0;">JS</h6> | |
<script> var r = parse_url("<?php echo $url; ?>");</script> | |
<pre><script>var_export(Object.keys(r).length)</script></pre> | |
<pre><script>var_export(r)</script></pre> | |
<?php } ?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment