Simonvetterli Posted March 1, 2013 Share Posted March 1, 2013 I have a simmilar same problem, hope somebody can help me: Depend of the tld I have I choose the language on the site: so I have the follow code: $domain = $_SERVER['SERVER_NAME']; list(subdom,$secdom,$tld) = explode(".",$domain); If I have in the browser www.turtlesdive.com no problem, ich I have www.turtlesdive.ch no problem. But if I have only turtlesdive.com or turtlesdive.ch - I not get any more com or ch as $tld. How can I do? Kindly Simon Quote Link to comment https://forums.phpfreaks.com/topic/275077-_serverserver_name-without-the-www/ Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 (edited) What you can do is either to check the count of elements, and if 2 then you know there is no sub-domain. Or you can use a regular expression, to match as required. In this case, I'd probably go for the RegExp, seeing as domains aren't quite as straight forward as they would seem. $domainRegExp = '(?P<sub>(?:[\\w\\pL][\\w\\pL-]*(?<!\\-)\\.)+)(?<!\\.|-)\\.'. '(?P<domain>[\\w\\pL][\\w\\pL-]+)(?<!\\.|-)\\.'. '(?P<tld>(??:priv|com?|me|org)\\.)?[a-z\\pL]{2,6})/ui';This will give you the three parts of a domain address in three named groups, "sub", "domain" and "tld". Which will give you named indices in the matches array, if used with preg_match. PS: Moved this to RegExp help, as this is very much regular expressions territory. PPS: Untested, so there might be bugs with this. If so, just let me know and I'll fix 'em. Edited March 1, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/275077-_serverserver_name-without-the-www/#findComment-1415768 Share on other sites More sharing options...
Simonvetterli Posted March 1, 2013 Author Share Posted March 1, 2013 Hi And how I do with my "problem": Here is my code: $lang = $_GET['lang']; $currency = ''; list($subdom,$secdom,$tld) = explode(".",$domain); if (($lang == '') AND ($tld == 'ch')){ $lang = 'ge'; $currency = 'CHF'; } elseif (($lang == 'ge') AND ($tld == 'ch')){ $lang = 'ge'; $currency = 'CHF'; } elseif (($lang == 'en') AND ($tld == 'ch')){ $lang = 'en'; $currency = 'CHF'; } elseif (($lang == '') AND ($tld == 'com')){ $lang = 'en'; $currency = 'THB'; } elseif (($lang == 'ge') AND ($tld == 'com')){ $lang = 'ge'; $currency = 'THB'; } $_GET['lang'] = $lang; $url1 = $_SERVER['PHP_SELF']; $url = basename($url1); How I "include" Your code? Kindly Simon Quote Link to comment https://forums.phpfreaks.com/topic/275077-_serverserver_name-without-the-www/#findComment-1415779 Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 (edited) Did you check the manual page for preg_match, which I linked you to? It is explained in great detail there how to use it to match a string against a regular expression. Also, after having a second look of the code it seems I forgot an important detail: Making the sub domain(s) optional. Here's the updated version: $domainRegExp = '(??P<sub>(?:[\\w\\pL][\\w\\pL-]*(?<!\\-)\\.)+)(?<!\\.|-)\\.)?'. '(?P<domain>[\\w\\pL][\\w\\pL-]*)(?<!\\.|-)\\.'. '(?P<tld>(??:priv|com?|me|org)\\.)?[a-z\\pL]{2,6})/ui'; Edited March 1, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/275077-_serverserver_name-without-the-www/#findComment-1415784 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.