Jump to content


Photo

Simple String Parising. Help!


  • Please log in to reply
2 replies to this topic

#1 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 30 June 2006 - 05:55 PM

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.somedomai...h/whatever.html
will become
http://www.somedomai.../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!

#2 technode

technode
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 June 2006 - 06:03 PM

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.

#3 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 30 June 2006 - 06:33 PM

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

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users