0
Personal opa,
I have a function that should identify a given url. If the url is identified, it should be involved with the tag <a>
and its property href
must be equal to the value of the url passed. In this case, I want only one url and your directories to be transformed into links, while other different url remain the same as they are.
Example: The url I want to link to whenever it appears in the text is: www.meusite.com.br
The problem I’m having is this: In my role, I’m not able to identify the url without http. What I need is to identify the url with or without the http protocol and have your href filled in correctly.
I need to identify the 3 cases below, but I can only identify the first.
http://www.meusite.com.br
www.meusite.com.br
meusite.com.br
Remember that urls other than this one should not be identified in the function filter. Ex: meusite2.com.br nay must be filtered
Follow my role with what I tried:
function checkUrl($text){
$pattern = "(https?)?://[a-z0-9./&?:=%-_]*";
preg_match_all($pattern, $text, $matches);
if( count($matches[0]) > 0 ){
foreach($matches[0] as $match){
$text = str_replace($match, "<a href='" . $match . "' target='_blank'>" . $match . "</a>", $text);
}
}
return $text;
}
$text = "Em http://www.meusite.com.br você encontra muitas dicas sobre o assunto. Na url www.meusite.com.br você encontra especialistas com seus artigos esclarecdores. E claro, meusite.com.br é o melhor site no assunto";
I believe my problem is only in the $Pattern variable, where I pass the pattern to be found.
I searched the forum here, but I couldn’t find exactly what I needed
Identify Urls and create links
The link above didn’t help, and I should do this in php.
With
http
is working properly?– Kenny Rafael
Yes. My problem is when I remove http from url. It stops recognizing :(
– DiChrist
I was just wondering 'cause in your code there’s
https
...– Kenny Rafael
that part there says that both http and https can be used. In this case the use of s would be optional.
– DiChrist
I drew,
RE
It’s not my strong...– Kenny Rafael
kkkk or mine, so I’m asking for help kkk
– DiChrist
hahaha, I think my answer should help...
– Kenny Rafael