This would omit the www
if they are present in the string:
function getSubdominio($host_array) {
$output = array_shift($host_array);
if ($output === 'www') {
$output = array_shift($host_array);
}
return $output;
}
With that declared function, you can replace this:
$subdomain = $host[0];
... for this:
$subdomain = getSubdominio($host);
** Edition:
The previous solution assumes that there is only one level of subdomains.
If there were more than one, the function would be more versatile like this:
function getSubdominio($host_array, $sub_level=1) {
$idx = 1 + $sub_level;
$inv_arr = array_reverse($host_array);
$output = $inv_arr[$idx];
if ($output === 'www') {
$idx += 1;
}
// Si se sale del límite, cogemos el último existente
while($idx >= sizeof($inv_arr) - 1)
{
$idx -= 1;
}
return $inv_arr[$idx];
}
With that, several levels of subdomains could be handled. For example, if you have:
logos.en.examples.com
To get en
:
$subdomain = getSubdominio($host, 1);
//El segundo parámetro es opcional y 1 por defecto
//Se puede omitir en este caso
$subdomain = getSubdominio($host);
And to get logos
:
$subdomain =getSubdominio($host, 2);