Con la expresión regular que detecte las URL en la cadena más el comentario de @emprear se puede lograr sacar el dominio correctamente.
Código PHP:
Ver originalfunction esip($ip_addr) {
//first of all the format of the ip address is matched
if (preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $ip_addr)) {
//now all the intger values are separated
//now we need to check each part can range from 0-255
foreach ($parts as $ip_parts) {
return FALSE; //if number is not within range of 0-255
}
return TRUE;
} else
return FALSE; //if format of ip address doesn't matches
}
function domain($domainb) {
if ($bits[0] == 'http:' || $bits[0] == 'https:') {
$domainb = $bits[2];
} else {
$domainb = $bits[0];
}
$idz -= 3;
if (strlen($bits[($idz + 2)]) == 2) { $url = $bits[$idz] . '.' . $bits[($idz + 1)] . '.' . $bits[($idz + 2)];
} else if (strlen($bits[($idz + 2)]) == 0) { $url = $bits[($idz)] . '.' . $bits[($idz + 1)];
} else {
$url = $bits[($idz + 1)] . '.' . $bits[($idz + 2)];
}
return $url;
}
$cadena = 'Este es mi url http://lavidaesbella.site.com y blablabla.';
preg_match('@(www\.|http://|([^\s]*\.[^\s]))([^\s|/]*)@i', $cadena, $m);
$address = $m[0];
$check = @esip($parsed_url['host']);
$host = @$parsed_url['host'];
if ($check == FALSE) {
if ($host != "") {
$host = domain($host);
} else {
$host = domain($address);
}
}
echo $host;