Cory94bailly Posted August 8, 2009 Share Posted August 8, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/169304-solved-cut-off-everything-after-the-last/ Share on other sites More sharing options...
.josh Posted August 8, 2009 Share Posted August 8, 2009 $string = preg_replace('~(^.*)/~','$1',$string); or a combo of strrchr and str_replace, though it might produce unexpected results... Quote Link to comment https://forums.phpfreaks.com/topic/169304-solved-cut-off-everything-after-the-last/#findComment-893396 Share on other sites More sharing options...
thebadbad Posted August 8, 2009 Share Posted August 8, 2009 Why not use substr() and strrpos()? $str = substr($str, 0, strrpos($str, '/')); Quote Link to comment https://forums.phpfreaks.com/topic/169304-solved-cut-off-everything-after-the-last/#findComment-893401 Share on other sites More sharing options...
Cory94bailly Posted August 8, 2009 Author Share Posted August 8, 2009 $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 Quote Link to comment https://forums.phpfreaks.com/topic/169304-solved-cut-off-everything-after-the-last/#findComment-893407 Share on other sites More sharing options...
.josh Posted August 8, 2009 Share Posted August 8, 2009 oops my bad. I was thinking of preg_match at the time. for preg_replace it would be: $string = preg_replace('~(^.+)/.+~','$1',$string); Quote Link to comment https://forums.phpfreaks.com/topic/169304-solved-cut-off-everything-after-the-last/#findComment-893461 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.