I wrote this to detect the subdomain, for me it works:
$HTTP_HOST = getenv("HTTP_HOST");
ereg("(www.)?([.]).([.]).([.]{2,5})", $HTTP_HOST, $regs)
$regs[2] contains the subdomain after running those two lines.
(just if you'rewondering: {2,5} is used because of my local, internal domain "local")
Regards,
Matthias