doumianfeng6979
doumianfeng6979
2013-05-16 17:04
浏览 100
已采纳

如何获取URL的查询字符串(问号后面的部分)?

How can I get only the content from a query string staring from ?:

asegment?param1=value1&param2=value2

NOTE: I only need all the content after the "?", also I want to ommit that character.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dougehe2022
    dougehe2022 2013-05-16 17:06
    已采纳

    Here is the Regex \?(.+), and a Rubular to prove it. And to use it:

    preg_match("/\?(.+)/", $inputString, $matches);
    if (!$matches) {
        // no matches
    }
    
    $result = $matches[1];
    
    点赞 评论
  • doufan3408
    doufan3408 2013-05-16 17:07

    You have a couple of options before that dont require regex like

    $val = (strpos("?", $url) !== false? substr($url, strpos($url, "?")): "");
    

    or parse_url

    $val = parse_url($url, PHP_URL_QUERY);
    

    EDIT

    Fixed condtition where there is not query string

    点赞 评论
  • duanke1984
    duanke1984 2013-05-16 17:07

    If you are sure your string has a query in it, then this will do the trick:

    echo substr( $string, strpos( $string, '?' ) + 1 );
    

    If your string may not contain the query, then check first - strpos will return boolean false.

    点赞 评论
  • dtdr84101
    dtdr84101 2013-05-16 17:11

    \?([a-z0-9A-z&=]+)*

    This looks like you're dealing with URLs, consider a library designed for them. And if you're only interested in getting everything after the ?, then use something like

    String s = "asegment?param1=value1&param2=value2";
    String paramStr = s.substring(s.indexOf('?')-1);
    

    Edit: Didn't see that you were using PHP - disregard.

    点赞 评论
  • dongpiao8821
    dongpiao8821 2013-05-16 17:19

    This is a simple solution using strrev and strtok...

    $url = "asegment?param1=value1&param2=value2";
    echo strrev(strtok(strrev($url),'?'));
    
    点赞 评论
  • dpauxqt1281
    dpauxqt1281 2013-05-16 17:22

    You should use $_GET global array <?php echo $_GET["param1"]; ?>

    点赞 评论

相关推荐