Get path from URL

2019-02-23 09:02发布

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

I only want the text after .com/

Can i do this with trim?

标签: php url
2条回答
Animai°情兽
2楼-- · 2019-02-23 09:25

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"
查看更多
劳资没心,怎么记你
3楼-- · 2019-02-23 09:27

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

$var='http://twitter.com/pwsdedtch';    
echo basename($var);
// pwsdedtech
查看更多
登录 后发表回答