One way to get the current domain is to get the value of the key SERVER_NAME
of array $_SERVER
.
SERVER_NAME: The name host of the server where the script is executed. If the script
is running in a host virtual, this will be the value set to
that host virtual.
Note: $_SERVER
is a array containing information such as headers, paths, and locations of script... There is no guarantee that each web server will provide some of these; servers may omit some, or provide others [..].
To extract information from a link, for example, the host, use the function parse_url
, and in a function you check whether the host extracted 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 perform the check, do so:
$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!";
}
Updating
In accordance with is response from Soen, use the regular expression below to extract links http/https:
(?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 function preg_match_all
to extract all the 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>";
}
}
It turns out, the content sent will not always be a link. It was just a simple example I gave, the content may vary between having links or not, so from
preg_replace
. So I can’t use your answer...– Igor
may come with
http
,https
or only thewww
.– Igor
@Igor Got it. Content can come with more than one link?
– stderr
Yes. @qmechanik
– Igor
No, because I need it to be with preg_match...
– Igor
@Igor Yes, I had updated the answer by placing the function
extrairLinks
who uses thepreg_match_all
, try a test and see if you can extract the(s) link(s) with that function.– stderr
@Igor I updated again, now the example puts the class internal/external depending on the link. If possible give the feedbeck about.
– stderr