Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path from URL

Tags:

url

php

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?

like image 453
CLiown Avatar asked Aug 24 '10 20:08

CLiown


2 Answers

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"
like image 198
zneak Avatar answered Oct 15 '22 05:10

zneak


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

$var='http://twitter.com/pwsdedtch';    
echo basename($var);
// pwsdedtech
like image 8
mpratt Avatar answered Oct 15 '22 03:10

mpratt