Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/275077-_serverserver_name-without-the-www/
Share on other sites

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 by Christian F.

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

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 by Christian F.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.