Personal opa,
I have a function that must identify a particular url. If the url is identified, it must be wrapped with the tag <a>
and its href
property must have the same value as the last url. In case I want only one url and its directories to be converted into links , while other urls remain the same as they are.
Example: The url that I want to link whenever it appears in the text is: www.mysite.com
The problem I'm having is the following: In my role, I'm not able to identify the url without the http. What I need is to identify the url with or without the http protocoloco and make the link have your href filled out 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
Remembering that urls other than this should not be identified in the function filter. Ex: miite2.com no should be filtered
Here's my function I've 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 think my problem is only in the $ pattern variable, where I pass the pattern to be found.
I searched here in the forum but did not find exactly what I needed
Identify URLs and create links
The above link did not help, and I should do this in php.