Web Hosting Talk







View Full Version : Help this PHP newbie! ;-)


diederik
09-19-2004, 12:35 PM
Hi, I hope you can help me with the following
I've got a string, which contains a subdomain and domain - like "whatever.domain.com".

What I want, is only use the domain (domain.com) and disregard the first part (whatever.) and call it something like $domain

After that, I want something like if $domain = yahoo.com then show yahoo.gif, if it is google.com show google.com, etc.

Hope I've been clear in my question ;)

null
09-19-2004, 12:41 PM
<?php

$string = "mail.yahoo.com";

$parts = explode(".", $string, 1);

$domain = $parts[1];

if ($domain == "yahoo.com")
// do something

?>

diederik
09-19-2004, 12:51 PM
Cool... thanks a lot!

MGHosted.net
09-19-2004, 01:26 PM
Don't forget the } after the // Do something. if ($domain == "yahoo.com") also needs a { on the end, making it this:


<?php

$string = "mail.yahoo.com";

$parts = explode(".", $string, 1);

$domain = $parts[1];

if ($domain == "yahoo.com") {
// do something
}

?>

diederik
09-19-2004, 02:11 PM
Just tried it in a script, but the $domain string seems to be empty, any ideas ?

the $string = build up as in the example (sub.domain.com)

edit : $parts[1] $parts[2],etc all are 'empty' - $parts[0] shows the full domain (sub.domain.com)

null
09-19-2004, 02:23 PM
My mistake, change

$parts = explode(".", $string, 1);

to

$parts = explode(".", $string, 2);

diederik
09-19-2004, 02:25 PM
Cool, it works now - you rock :)

Burhan
09-20-2004, 02:13 AM
There is a better function for this, because with your current solution, you will run into issues with addresses such as http://www.yahoo.com.

Instead, use http://www.php.net/parse_url