【问题标题】:PHP's "parse_url" in JavaScriptJavaScript 中 PHP 的“parse_url”
【发布时间】:2013-07-21 02:16:07
【问题描述】:

我写了一个 PHP 的 parse_url 函数的 JavaScript 副本。

就我现在所写的而言,该函数完成了这项工作并返回了一个 JS 对象...

但是有没有更好的方法来做到这一点:

function parse_url( url ) {
    if(typeof url == 'string') {
        // output following: scheme, host, user, pass, path, query, fragment
        var output = {};

        var split_scheme = url.split('//');

        // now we assume that we have: sheme, and the rest of the url after //
        if(split_scheme.length == 2) {
            // now we have the "scheme"
            // do not add if this URL is provided: //hostname/path?query=value#anchor
            if(split_scheme[0].length) {
                output.scheme = split_scheme[0].replace(':', '');
            }

            // we're now splitting the URL on first slash /
            // and assume that we'll get: host, (user and pass, if any);

            var split_url = split_scheme[1].split('/');

            if(split_url.length == 2) {
                // check if user/pass are provided
                var split_auth_hostname = split_url[0].split('@');

                output.host = split_auth_hostname[1];

                if(split_auth_hostname.length == 2) {
                    // now split the auth part of the hostname with ":"
                    var split_user_info = split_auth_hostname[0].split(':');

                    if(split_user_info.length == 2) {
                        // assume that both user and pass are provided now
                        output.user = split_user_info[0];
                        output.pass = split_user_info[1];
                    } else {
                        // assume that only "user" is provided
                        output.user = split_user_info[0];
                    }
                } else {
                    // assume that no auth info was provided in the URL
                    // first splitted element is the actual hostname
                    output.host = split_auth_hostname[0];
                }

                // now let's split the query/anchor from path
                var split_query = split_url[1].split('?');

                output.path = '/' + split_query[0];

                if(split_query.length == 2) {
                    // now split the anchor out of query string
                    var split_anchor = split_query[1].split('#');

                    // add the query without anchor
                    output.query = split_anchor[0];

                    // add anchoer
                    if(split_anchor.length == 2) {
                        output.fragment = '#' + split_anchor[1];
                    }
                } else {
                    output.query = split_query[0];
                }
            }
        }

        return output;
    }
}

我创建了一个demo jsfiddle here

【问题讨论】:

  • 这个问题更适合codereview.stackexchange.com
  • 我没有花时间阅读您的代码,但 javascript 中的 window.location 对象给出了 PHP parse_url 所做的部分 url。为什么要重新创建相同的行为?
  • @nt.bas - 教育目的。
  • 好的。我相信正则表达式会更快(只要您使用文字符号而不是 new 运算符创建它们。)但是到目前为止我只是挥手,因为我没有进行任何基准测试。正在努力...与此同时,@Pieter 的链接值得您查看。

标签: php javascript url-parameters parse-url


【解决方案1】:

jsPerf 的性能测试表明使用正则表达式会更快。

我使用了来自 javascript 的正则表达式。这是一个教科书示例,它的运行速度比您的代码快 73%,因此希望生产质量代码在最坏的情况下做得更好和平等。

基准网址http://jsperf.com/parse-url

而使用的代码是:

function parse_url_regex(url) {
  var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;

  var result = parse_url.exec(url);

  return result;

}

但我强烈建议您尝试 codereview.stackexchange.com

【讨论】:

  • 这不是更好的方法,因为它不提供相同的结构,而且如果没有订单描述,它输出的结构也没有任何意义。
猜你喜欢
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多