Get path from url

Looking for a way to get the path from a URL in PHP:

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

And do it: hurrdurr

I need text only after .com/

Can I do this with cropping?

+4
source share
2 answers

Use parse_url to extract the information you need.

For instance:

 $url = "http://twitter.com/pwsdedtch"; $path = parse_url($url, PHP_URL_PATH); // gives "/pwsdedtech" $pathWithoutSlash = substr($path, 1); // gives "pwsdedtech" 
+17
source

In this particular case, you can also use "basename" for your own purposes.

 $var='http://twitter.com/pwsdedtch'; echo basename($var); // pwsdedtech 
+7
source

All Articles