getting variables from STRING url in php

后端 未结 5 2066
情深已故
情深已故 2021-01-20 11:34

If I have a url that looks like this, what\'s the best way to read the value

http://www.domain.com/compute?value=2838

I tried parse_u

相关标签:
5条回答
  • 2021-01-20 11:54

    You can use parse_url and then parse_str on the query.

    <?php
    $url = "http://www.domain.com/compute?value=2838";
    $query = parse_url($url, PHP_URL_QUERY);
    $vars = array();
    parse_str($query, $vars);
    print_r($vars);
    ?>
    

    Prints:

    Array
    (
        [value] => 2838
    )
    
    0 讨论(0)
  • 2021-01-20 11:58

    parse_str($url) will give you $value = 2838.

    See http://php.net/manual/en/function.parse-str.php

    0 讨论(0)
  • 2021-01-20 12:00

    You should use GET method e.g

    echo "value = ".$_GET['value'];  
    
    0 讨论(0)
  • 2021-01-20 12:06

    For http://www.domain.com/compute?value=2838 you would use $_GET['value'] to return 2838

    0 讨论(0)
  • 2021-01-20 12:08
    $uri = parse_url($uri);
    parse_str($uri['query'], $params = array());
    

    Be careful if you use parse_str() without a second parameter. This may overwrite variables in your script!

    0 讨论(0)
提交回复
热议问题