ERRO ao transformar parte da String em URL (CI)
Pessoal, estou desenvolvendo uma pequena rede social, e preciso fazer com que alguns campos transformem parte de uma string em uma URL, fiz uma function para isso, a mesma funciona em partes.
Se observarem, se eu colocar 2 ou mais URLS com http:// na mesma string, ele reconhece como apenas URL.
$texto = 'http://teste.com e http://google.com e www.globo.com'
echo texto($string)
Function PHP (CodeIgniter)
function texto($texto) {
$er = "/(https:\/\/(www\.|.*?\/)?|http:\/\/(www\.|.*?\/)?|www\.)([a-zA-Z0-9]+|_|-)+(\.(([0-9a-zA-Z]|-|_|\/|\?|=|&)+))+/i";
$texto = preg_replace_callback($er, function($match){
$link = $match[0];
$link = (stristr($link, "http") === false) ? "http://" . $link : $link;
$link = str_replace ("&", "&", $link);
return "<a href='".strtolower($link)."' target='_blank'>".str_replace('http://', '', $link)."</a>";
}, $texto);
return $texto;
}
Retorno HTML (return $texto):
<a href='http://teste.com e http://google.com' target='_blank'>teste.com e google.com</a> e <a href='http://www.globo.com' target='_blank'>www.globo.com</a>Discussão (0)
Carregando comentários...