Jump to content

Archived

This topic is now archived and is closed to further replies.

bpops

Simple String Parising. Help!

Recommended Posts

Ok, so I'm working on some code that will take a URL and display just the host and path (minus the page itself.) For example, given:

http://www.somedomain.com/blahblah/blah/whatever.html
will become
http://www.somedomain.com/blahblah/blah/

I've tried parse_url, but the problem is that will give the host (www.somedomain.com) and path (/blahblah/blah/whatever.html).
So it is of no use here.

I'm sure I can do this just by somehow having PHP look for the last forward slash, "/" in the string and truncating after that. Can someone help me out witha bit of code that might do this?

Thanks in advance!

Share this post


Link to post
Share on other sites
I did this by explode. this way all the items are in elements in an array (split by "/"). If your file is always in the last element you can rebuild the string  minus the last element.

Share this post


Link to post
Share on other sites
So, would this work?
[code]
$url = 'http://www.somedomain.com/blah/blah/whatever.html'
$url_arr = explode("/", $url)
$page = array_pop($url_arr)
$new_url = $implode("/",$url_arr)
[/code]

edit: the code is very small to read :P.    pasted:

$url = 'http://www.somedomain.com/blah/blah/whatever.html'
$url_arr = explode("/", $url)
$page = array_pop($url_arr)
$new_url = $implode("/",$url_arr)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.