Improve company productivity with a Business Account.Sign Up

x
?
Solved

Get the sub-domain name and verify configuration

Posted on 2010-09-22
11
Medium Priority
?
715 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
10 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 4

Accepted Solution

by:
trencH87 earned 700 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 31

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
 
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 700 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 31

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 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 700 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

606 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