PHP Parse URL From Current URL

前端 未结 6 949
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 05:38

I am trying to parse url and extract value from it .My url value is www.mysite.com/register/?referredby=admin. I want to get value admin from this url.

相关标签:
6条回答
  • 2021-01-29 05:52
    $referred = "referredby=admin";
    $pieces = explode("=", $referred);
    echo $pieces[1]; // admin
    
    0 讨论(0)
  • 2021-01-29 05:53
    $referred = $_GET['referredby'];
    
    0 讨论(0)
  • 2021-01-29 06:01

    Is not a really clean solution, but you can try something like:

    $url = "MYURL";
    $parse = parse_url($url);
    parse_str($parse['query']);
    
    echo $referredby; // same name of the get param (see parse_str doc)
    

    PHP.net: Warning

    Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.

    Dynamically setting variables in function's scope suffers from exactly same problems as register_globals.

    Read section on security of Using Register Globals explaining why it is dangerous.

    0 讨论(0)
  • 2021-01-29 06:04

    You can use parse_str() function.

    $url = "www.mysite.com/register/?email=admin";
    $parts = parse_url($url);
    parse_str($parts['query'], $query);
    echo $query['email'];
    
    0 讨论(0)
  • 2021-01-29 06:06

    Try this code,

    $url = "www.mysite.com/register/?referredby=admin";
    $parse = parse_url($url, PHP_URL_QUERY);
    parse_str($parse, $output);
    echo $output['referredby'];

    0 讨论(0)
  • 2021-01-29 06:08

    I don't know if it's still relevant for you, but maybe it is for others: I've recently released a composer package for parsing urls (https://www.crwlr.software/packages/url). Using that library you can do it like this:

    $url = 'https://www.example.com/register/?referredby=admin';
    $query = Crwlr\Url\Url::parse($url)->queryArray();
    echo $query['referredby'];
    

    The parse method parses the url and returns an object, the queryArray method returns the url query as array.

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