Jump to content

[SOLVED] Cut off everything after the LAST "/" ?


Cory94bailly

Recommended Posts

Hey guys.. I have a url and I want to take out everything past the last "/" (including the /)

 

Example: http://mysite.com/forums -changes to- http://mysite.com

                http://mysite.com/testing/2/forums -changes to- http://mysite.com/testing/2

 

 

 

I know this should not be very hard.. ;)

$string = preg_replace('~(^.*)/~','$1',$string);

 

or a combo of strrchr and str_replace, though it might produce unexpected results...

 

That just turned out to change it to "http://mysite.comforums"

 

 

 

Why not use substr() and strrpos()?

 

$str = substr($str, 0, strrpos($str, '/'));

That worked perfectly! ;)

 

Thanks

Archived

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

×
×
  • 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.