Solved

Get the sub-domain name and verify configuration

Posted on 2010-09-22
11
696 Views
Last Modified: 2012-06-27
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
Comment
Question by:Pedro Chagas
  • 4
  • 3
  • 3
11 Comments
 
LVL 4

Expert Comment

by:trencH87
ID: 33737966
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
 
LVL 4

Expert Comment

by:trencH87
ID: 33737993
If you ONLY want to allow 2 - TWO - letters in subdomain, change >= to == in the IF statement.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33739273
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
 
LVL 4

Accepted Solution

by:
trencH87 earned 175 total points
ID: 33741069
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
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33741160
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33746803
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
 
LVL 4

Assisted Solution

by:trencH87
trencH87 earned 175 total points
ID: 33747415
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
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33747762
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33749134
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
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 175 total points
ID: 33751034
I'm very sorry for my inattention:

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

Best
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now