bpops Posted June 30, 2006 Share Posted June 30, 2006 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.htmlwill becomehttp://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! Quote Link to comment Share on other sites More sharing options...
technode Posted June 30, 2006 Share Posted June 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
bpops Posted June 30, 2006 Author Share Posted June 30, 2006 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.