Forum Moderators: coopster

Message Too Old, No Replies

help splitting a param into variables

         

lindajames

2:52 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



Hi,

I have a domain checking php script that is called like this whois.php?domain=mydomain.com

can anyone tell me how i can split the value of the domain parameter into 2 different variables. 1 as domain and the other as the extension so that i can print the value of domain just by putting $domain and print the extension type by putting $ext

any help would be appreciated

cheers

daisho

2:55 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



PHP's explode() function [php.net] and explode on "." dots.

daisho.

lindajames

2:57 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



if i split it on the dots the when i have domains like .co.uk it will chop of the .uk bit of it. isnt there any other ways?

coopster

3:12 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have a look at the parse_url function [us4.php.net].

daisho

3:14 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



Not really. PHP knows nothing of domains and subdomains. You must add that logic yourself.

You could search for the first dot rather than split on all dots. Or look into writing a regular expression.

daisho.

lindajames

5:05 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



i've tried the following:

<?php

$domain = explode(".", $_GET[domain]);
print "Domain: $domain[0]";
print "<br>";
print "Extension: $domain[1]";

?>

when i put?domain=something.com it works fine, but if there are 2 dots in the domain .e.g .co.uk then it only displays the co

any help would be appreciated.

cheers

daisho

5:34 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



if you trust there is no 4th level (ie www.domain.co.uk) then break on the first "." using strpos() [php.net] and substr() [php.net].

or create a static list of domains with subdomains ie:

$tld['uk']['co']=1;

Then when you do your explode ie:

$parts=explode(".",$domain);

if( isset( $tld[$parts[ count($parts-1) ] ] ) ) {
// more logic here.
}

because you cannot always garantee that it will be 1 or 2 levels. ie. Here in Canada we have .ca we also have .on.ca, .qc.ca, .bc.ca and so on for every province. A site could either have a .ca or a .prov.ca. It may be that way for other TLDs. If you want your solutions to work properly then you have to account from this if you deal with the url from the right hand side.

Like I said earlier if the first part of the domain will *always* be the domain name then you can deal with it from the lefthand side. ie everything before the first "." is the domain. Everything after the first "." is the TLD.

daisho.

lindajames

5:54 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



isnt there a simple way of just getting it to split everything before the first dot and everything after that?

daisho

6:03 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



like I said in the previous post. strpos and substr.

coopster

9:21 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry, parse_url won't give you what you want. And daisho is correct, trying to get down to subdivision/subdomain is going to be tough! RFC1591 [ietf.org] will give you an idea of what you might be facing. However, if you want to have a go at it, this should give you a start. Just keep in mind what daisho said in post#7 regarding levels:

<?php
$subdomains = array('AC', 'CO', 'GO', 'RE' [ietf.org]); // you fill this!

$url='http://www.domain.co.uk'; // or 'domain.co.uk' -- works for either
if (false!== strpos($url, ".")) { // make sure there is a period
$values = explode(".", $url); // separate values on the periods
// use the last two, or last one, depending on what the second to last is:
$ext = (in_array(strtoupper($values[count($values)-2]), $subdomains))
? $values[count($values)-2].'.'.$values[count($values)-1]
: $values[count($values)-1];
} else {
// processing if no periods were found...
}
?>

Timotheos

8:34 pm on Sep 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's some talk of this here. [webmasterworld.com]

lindajames

11:29 am on Sep 27, 2003 (gmt 0)

10+ Year Member



i've done it like this and it seems to work:

<?php
$domainparam = $_GET[domain];

$tld = substr($domainparam, strpos($domainparam, ".") + 1);
$domain = explode(".", $domainparam);

print "Domain: $domain[0]";
print "<br>";
print "Extension: $tld";

?>

is there anything wrong with it?

coopster

3:20 pm on Sep 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It all depends on how the information is being put into the variable you have named domain. If you are using a form, and getting the input from an end user, do you trust the information being passed to you is always in the correct format? ...which seems to be, in your case, domain followed by extension, whether it is a single subdomain, or variable subdomains? If you know for sure that the information up to the first period (.) is going to be what you expect, your code would work as you want it to.