Hola tengo esta funcion:
Código PHP:
Ver originalfunction textToLink($subject){
$pattern = "/(?i)\b(((http|https|ftp):\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
$text = preg_replace($pattern, "<a href=\"$1\" target=\"_blank\">$1</a>", $subject); $text = str_replace("href=\"www.","href=\"http://www.",$text); return $text;
}
Esa funcion anda bien de esta manera
Código PHP:
Ver original$subject = 'link1 http://www.youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
link2 www.youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
link3 https://www.youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
link4 ftp://youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
link5 snipplr.com/view/64247/';
$texto = textToLink($subject);
Funciona bien para los 4 primeros links, pero para el 5 retorna
Código HTML:
Ver original<a href="snipplr.com/view/64247/" target="_blank">snipplr.com/view/64247/
</a>
y necesito que me retorne
Código HTML:
Ver original<a href="http://snipplr.com/view/64247/" target="_blank">snipplr.com/view/64247/
</a>
He tratado con varias expresiones regulares que me verifican si existe un a href sin http, ftp o https, pero a la hora de remplazarlo no funciona como espero
Código PHP:
Ver originalpreg_replace('/(?i)\b(<a href="[^http:\/\/]|[^https:\/\/]|[^ftp:\/\/][a-z0-9.\-]+[.]")/',"<a href=\"http://$1\" target=\"_blank\">$1</a>",$text)
Hay alguna sugerencia de como hacerlo?
Saludos