Brandon_R Posted September 28, 2009 Share Posted September 28, 2009 Hello guys i need a bit of help with this. I need a regex to extract the base URL from a long url. Please take a look at the following example: http://www.example.com/1-link-to-the-page.html after the regex is run on the above code $url should contain http://www.example.com/ Thank You In Advance for your help Quote Link to comment https://forums.phpfreaks.com/topic/175838-extract-base-url-from-entire-url/ Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 You don't necessarily need regex for this. If the URL is always going to be in that format you could do this: $url = 'http://www.example.com/1-link-to-the-page.html'; echo implode('/', explode('/', $url, -1)) . '/'; Quote Link to comment https://forums.phpfreaks.com/topic/175838-extract-base-url-from-entire-url/#findComment-926554 Share on other sites More sharing options...
thebadbad Posted September 28, 2009 Share Posted September 28, 2009 parse_url() are made for this: $parts = parse_url('http://www.example.com/1-link-to-the-page.html'); $url = $parts['scheme'] . '://' . $parts['host']; Quote Link to comment https://forums.phpfreaks.com/topic/175838-extract-base-url-from-entire-url/#findComment-926574 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.