Mcod Posted January 10, 2012 Share Posted January 10, 2012 Hello, I am trying to find out how to remove everything from a string after the third slash. Example: http://domain.com/test.html would become http://domain.com/ one/two/three/four/five/ would become one/two/three/ Your help is greatly appreciated Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/254738-remove-everything-from-a-string-after-the-third-slash/ Share on other sites More sharing options...
JAY6390 Posted January 10, 2012 Share Posted January 10, 2012 You would be better off using parse_url <?php $text = 'http://www.somedomain.com/blah/blah/blah'; $parts = parse_url($text); echo $parts['scheme'] . '://' . $parts['host'] . '/'; For the regex version, this will do it <?php $text = 'http://www.somedomain.com/blah/blah/blah'; $result = preg_replace('~^(.*?//.*?/).*$~', '$1', $text); echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/254738-remove-everything-from-a-string-after-the-third-slash/#findComment-1306198 Share on other sites More sharing options...
ManiacDan Posted January 10, 2012 Share Posted January 10, 2012 Neither of the above will handle your second case. You want: echo preg_replace('#^(.*?/.*?/.*?/).*$#', "\\1", $yourString); -Dan Quote Link to comment https://forums.phpfreaks.com/topic/254738-remove-everything-from-a-string-after-the-third-slash/#findComment-1306203 Share on other sites More sharing options...
.josh Posted January 10, 2012 Share Posted January 10, 2012 non-regex on-liner alternative: $string = implode("/",array_slice(explode("/",$string,4),0,3)); Quote Link to comment https://forums.phpfreaks.com/topic/254738-remove-everything-from-a-string-after-the-third-slash/#findComment-1306210 Share on other sites More sharing options...
ManiacDan Posted January 10, 2012 Share Posted January 10, 2012 Josh's way is faster than mine, but he trims the trailing slash. You'll have to re-add it. Quote Link to comment https://forums.phpfreaks.com/topic/254738-remove-everything-from-a-string-after-the-third-slash/#findComment-1306211 Share on other sites More sharing options...
Pikachu2000 Posted January 10, 2012 Share Posted January 10, 2012 If the string is coming from a database, you could also do it in the query string with this SELECT CONCAT( SUBSTRING_INDEX(`field_name`, '/', 3), '/' ) Quote Link to comment https://forums.phpfreaks.com/topic/254738-remove-everything-from-a-string-after-the-third-slash/#findComment-1306213 Share on other sites More sharing options...
Mcod Posted January 11, 2012 Author Share Posted January 11, 2012 Thank you for all your replies - got it working now thanks to you all What a wonderful forum Quote Link to comment https://forums.phpfreaks.com/topic/254738-remove-everything-from-a-string-after-the-third-slash/#findComment-1306699 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.