Get path from URL

后端 未结 2 1282
后悔当初
后悔当初 2021-01-18 09:42

Looking for a way of getting the path from an URL in PHP:

I want to take: http://example.com/hurrdurr

And make it: hurrdurr

相关标签:
2条回答
  • 2021-01-18 10:07

    For this particular case you could also use "basename" for your purposes.

    $var='http://twitter.com/pwsdedtch';    
    echo basename($var);
    // pwsdedtech
    
    0 讨论(0)
  • 2021-01-18 10:28

    Use parse_url to extract the information you want.

    For instance:

    $url = "http://twitter.com/pwsdedtch";
    $path = parse_url($url, PHP_URL_PATH); // gives "/pwsdedtech"
    $pathWithoutSlash = substr($path, 1); // gives "pwsdedtech"
    
    0 讨论(0)
提交回复
热议问题