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.. 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... 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, '/')); 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 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); 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
Archived
This topic is now archived and is closed to further replies.