[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 714
  • Last Modified:

Get the sub-domain name and verify configuration

I E's, I want to get the name of sub-domain, and at the same time verify if the sub-domain is well written.
The sub-domains just can contain 2 alpha characters, like pt, es, en, it etc.
In practice if I have this domains:
http://pt/domain.com/ or http://www.pt/domain.com/
http://es/domain.com/index.php or http://www.es/domain.com/index.php
http://en/domain.com/index.php?variables or http://www.en/domain.com/index.php?variables

The output should be pt, es or en, and the configuration of 'sub-domain' was correct.

Note: the only thing I want to know it was the configuration of sub-domain, not the entirely URL.

So, How I get sub-domain acronym and how I see if sub-domain have 2 alpha characters?

Regards, JC

0
Pedro Chagas
Asked:
Pedro Chagas
  • 4
  • 3
  • 3
3 Solutions
 
trencH87Commented:
Im guessing you mean http://pt.domain.com or http://www.pt.domain.com and not pt/domain.com and so on. Try this code:
$url = "http://pt.domain.com";

$pieces = explode(".",$url);
$check = (substr($pieces[0],0,4) == "http") ? substr($pieces[0],7) : substr($pieces[0],0);
$subdomain = ($check == "www") ? $check.".".$pieces[1] : $check;
if (strstr($subdomain,".")) {
	$sub = substr($subdomain,4);
	if (strlen($sub) >= 2) {
		echo $subdomain." : Success!";
	} else {
		echo $subdomain." : Failed!";
	}
} else {
	if (strlen($subdomain) >= 2) {
		echo $subdomain." : Success!";
	} else {
		echo $subdomain." : Failed!";
	}
}

Open in new window

0
 
trencH87Commented:
If you ONLY want to allow 2 - TWO - letters in subdomain, change >= to == in the IF statement.
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi @trencH87, you try your solution before you post them?
Don't work well, with >= or ==, don't work any way.

The first failure was the kind of characters, like I tell before, the characters just can be alpha, so don't accept numeric characters.

Another failure was with or without www., if I write http://pt.domain.com the output is pt: success! but if I write http://www.pt.domain.com the output is www.pt: success, when the correct is just pt: success!

Note: I change the lines >= for == for made the tests.

Regards, JC
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
trencH87Commented:
Then try this:
$url = "http://pt.domain.com";

$pieces = explode(".",$url);
$check = (substr($pieces[0],0,4) == "http") ? substr($pieces[0],7) : substr($pieces[0],0);
$subdomain = ($check == "www") ? $check.".".$pieces[1] : $check;
if (strstr($subdomain,".")) {
	$sub = substr($subdomain,4);
	if (strlen($sub) == 2 && ctype_alpha($sub)) {
		echo $sub." : Success!";
	} else {
		echo $sub." : Failed!";
	}
} else {
	if (strlen($subdomain) == 2 && ctype_alpha($subdomain)) {
		echo $subdomain." : Success!";
	} else {
		echo $subdomain." : Failed!";
	}
}

Open in new window

0
 
Marco GasiFreelancerCommented:
You can try with preg_match using this regex:
$url = one of url you posted above
preg_match("/(?<=http:\/\/)(\w\w)(?=\/)|(?<=http:\/\/www\.)/" $url, $mathces);
echo $matches[0];

Cheers
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi @marqusG, I try your code, but don't work. I use like you can see in snippet code. Maybe I do something wrong.
Please check. I will waiting for your new solution (off course in until time) for close the question.

Hi @trencH87, your code is correct.

Regards, JC


<?
$string = "http://www.pt.domain.com";
preg_match("/(?<=http:\/\/)(\w\w)(?=\/)|(?<=http:\/\/www\.)/", $string, $match);
if($match[0] == ""){
	echo "Wrong URL";
} else {
	echo "The URL:$match[0] is correct";
}
?>

Open in new window

0
 
trencH87Commented:
In a function:
function checkSub($url) 
{
	$pieces = explode(".",$url);
	$check = (substr($pieces[0],0,4) == "http") ? substr($pieces[0],7) : $pieces[0];
	$subdomain = ($check == "www") ? $check.".".$pieces[1] : $check;
	$sub = substr($subdomain,4);
	
	if (strstr($subdomain,".")) {
		$msg = (strlen($sub) == 2 && ctype_alpha($sub)) ? $sub." : Success!" : $sub." : Failed!";
	} else {
		$msg = (strlen($subdomain) == 2 && ctype_alpha($subdomain)) ? $subdomain." : Success!" : $subdomain." : Failed!" ;
	}
	
	return $msg;
}

$subCheck = checkSub("http://pt.domain.com");
echo $subCheck;

Open in new window

0
 
Marco GasiFreelancerCommented:
Fixed (a little change to regexp, the first because is more correct this way, the seond one to adjust to match subdomain if foolowed by a dot - in your first post subdomain was never followed by a dot):

<?php
$string = "http://www.pt.domain.com";
preg_match("/(?<=http:\/\/|http:\/\/www\.)(\w\w)(?=\/|\.)/", $string, $match);
if($match[0] == ""){
      echo "Wrong URL";
} else {
      echo "The URL:$match[0] is correct";
}
?>

Cheers
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi again @marqusG, just one more pitch, which had already mentioned in earlier posts.
Like I tell before, the acronym just have 2 characters and this characters have to be alpha, not numeric. If I write: $string = "http://p3.domain.com"; the script recognize with a correct configuration, when is wrong configuration. Sub-domain just accept alpha characters (a-Z).

Hi trencH87, your example still correct (now a function).

Regards, JC
0
 
Marco GasiFreelancerCommented:
I'm very sorry for my inattention:

preg_match("/(?<=http:\/\/|http:\/\/www\.)[a-zA-Z]{2}(?=\/|\.)/", $string, $match);

Best
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now