Jump to content

Simple String Parising. Help!


bpops

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!
Link to comment
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)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.