Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get the sub-domain name and verify configuration

Posted on 2010-09-22
11
Medium Priority
?
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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