One way to get the current domain is to get the key value SERVER_NAME
from the array $_SERVER
.
SERVER_NAME : The host name of the server where the current script is run. If the script
is running on a virtual host , this will be the value set for
that virtual host.
Note : $_SERVER
is an array that contains information such as headers, paths, and script ... There is no guarantee that each web server will provide any of these; servers may omit some, or provide others [..].
To extract information from a link , for example, host , use function parse_url
, and in a function you check if the extracted host is equivalent or not to your site:
function verificarLink($link, $dominio) {
$info = parse_url($link);
$host = isset($info['host']) ? $info['host'] : "";
return ((!empty($host) && strcasecmp($host, $dominio) == 0) ? true : false);
}
To do this, do the following:
$link = "http://www.site.com.br/teste!apenasumteste";
$dominio = $_SERVER['SERVER_NAME'];
if (verificarLink($link, $dominio)) {
echo "Domínio interno!";
} else {
echo "Domínio externo!";
}
Update
As this SOEn response, answer, use the regular expression below to extract links link :
(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s'!()\[\]{};:'\".,<>?«»“”‘’]))
This regular expression can be used in the preg_match_all
function to extract all links of a string :
function extrairLinks($conteudo){
$expressao = "%(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s'!()\[\]{};:'\".,<>?«»“”‘’]))%";
preg_match_all($expressao, $conteudo, $resultados);
$links = array_filter(array_map('array_filter', $resultados))[0]; // Remover capturas vazias
return $links;
}
And to use it do:
$dominio = $_SERVER['SERVER_NAME'];
$links = extrairLinks($conteudo);
foreach($links as $link){
if (verificarLink($link, $dominio)) {
echo '<a class="link_interno" href="'. $link .'" target="_blank">'. $link .'</a>' . "<br>";
} else {
echo '<a class="link_externo" href="'. $link .'" target="_blank">'. $link .'</a>' . "<br>";
}
}