simonp Posted April 30, 2010 Share Posted April 30, 2010 Hi, I need to be able to split a domain name into two variables - the bit before the 'dot' and the bit after eg microsoft.com I need domain = microsoft tld = com (there will always be only one 'dot') What's the best way to do this? Thanks Simon Link to comment https://forums.phpfreaks.com/topic/200259-split-a-variable-at-the-dot-domain-name/ Share on other sites More sharing options...
cags Posted April 30, 2010 Share Posted April 30, 2010 $input = 'microsoft.com'; $domain = strtok($input, '.'); $tld = strtok(''); Link to comment https://forums.phpfreaks.com/topic/200259-split-a-variable-at-the-dot-domain-name/#findComment-1050936 Share on other sites More sharing options...
OOP Posted April 30, 2010 Share Posted April 30, 2010 $url = "microsoft.com"; $url_parts = explode('.',$url); print_r($url_parts); Link to comment https://forums.phpfreaks.com/topic/200259-split-a-variable-at-the-dot-domain-name/#findComment-1050939 Share on other sites More sharing options...
teamatomic Posted April 30, 2010 Share Posted April 30, 2010 As you can see there are many ways to do what you wish. list($domain,$tld) = explode('.',$url); if the results of explode could be varying, as if you really dont know how many dots the url would have then an array is good as you can work through it figuring out what you have. If, as you state, the url is a known and constant then it is easier to explode and assign the parts to variables all in one step. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/200259-split-a-variable-at-the-dot-domain-name/#findComment-1051047 Share on other sites More sharing options...
simonp Posted April 30, 2010 Author Share Posted April 30, 2010 Thanks guys - I went with the strtok which worked great. Simon Link to comment https://forums.phpfreaks.com/topic/200259-split-a-variable-at-the-dot-domain-name/#findComment-1051050 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.