-Karl- Posted October 22, 2012 Share Posted October 22, 2012 (edited) Hello, I'm having difficulty extracting the root domain from an URL, whilst excluding a subdomain if one is there. Basically: $txt = 'http://i.imgur.com'; $parse = parse_url($txt); $url = $parse['host']; Returns i.imgur.com, but I only need imgur.com. However, the reason it's complicated is because the url could also be www.imgur.com, so you can't simply explode by ".", then there's also the problem with other tld's. For example, m465.imageshack.us, all I need it to return is imageshack.us. What would be the best way to achieve this? Edited October 22, 2012 by -Karl- Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 22, 2012 Share Posted October 22, 2012 Couldn't you explode the value as mentioned to get the TLD. Then just test if the first part contains "www"? Quote Link to comment Share on other sites More sharing options...
-Karl- Posted October 22, 2012 Author Share Posted October 22, 2012 Couldn't you explode the value as mentioned to get the TLD. Then just test if the first part contains "www"? Not really, since 456.imageshack.us/image.png contains three periods, whereas tinypic.com/image.png contains two. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 22, 2012 Share Posted October 22, 2012 That would be another test. If the array created by explode contains two items, you have what you need. If it has more, check if the first one is "www". Quote Link to comment Share on other sites More sharing options...
shaddowman Posted October 22, 2012 Share Posted October 22, 2012 I think the best possible solution is the use of built in PHP function parse_url. You can extract the domain name and might get the exact domain name by the help of string manipulator functions susch as substr() and strpos() <?php $url = parse_url("http://123.456.imageshack.us/image.png"); print_r($url); ?> Quote Link to comment Share on other sites More sharing options...
-Karl- Posted October 22, 2012 Author Share Posted October 22, 2012 I think the best possible solution is the use of built in PHP function parse_url. You can extract the domain name and might get the exact domain name by the help of string manipulator functions susch as substr() and strpos() <?php $url = parse_url("http://123.456.imageshack.us/image.png"); print_r($url); ?> If you read my first post, parse_url() is exactly what I am using... I then explained the reason why it's not suffice. Quote Link to comment Share on other sites More sharing options...
-Karl- Posted October 22, 2012 Author Share Posted October 22, 2012 (edited) Ended up doing an explode: $parse = parse_url($image); $url = $parse['host']; $explode = explode('.',$url); if(count($explode) > 2) { $url = $explode[1].'.'.$explode[2]; } Just need to do some testing for all possible variables now. Edited October 22, 2012 by -Karl- Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 22, 2012 Share Posted October 22, 2012 (edited) $parsed = parse_url($image); $host_parts = explode('.', $parsed['host']); $domain = implode('.', array_slice($host_parts, count($host_parts)-2)); Edited October 22, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
ignace Posted October 22, 2012 Share Posted October 22, 2012 (edited) Based off Psycho's solution. You can pass a PHP_URL_HOST constant to parse_url also you can pass a negative value as offset to array_slice. $image = 'http://123.456.imageshack.us/image.png'; $domain = implode('.', array_slice(explode('.', parse_url($image, PHP_URL_HOST)), -2)); // imageshack.us Edited October 22, 2012 by ignace Quote Link to comment Share on other sites More sharing options...
kicken Posted October 22, 2012 Share Posted October 22, 2012 Note that some extensions have multiple periods (eg bbc.co.uk). If you're concerned about domains like that you'll need to create a list of all the possible TLDs to test against. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 22, 2012 Share Posted October 22, 2012 . . . also you can pass a negative value as offset to array_slice I should have known that. Quote Link to comment 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.