Tema: Link con php
Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/11/2012, 15:21
Avatar de jonni09lo
jonni09lo
Colaborador
 
Fecha de Ingreso: septiembre-2011
Ubicación: Estigia
Mensajes: 1.471
Antigüedad: 12 años, 7 meses
Puntos: 397
Link con php

Hola tengo esta funcion:

Código PHP:
Ver original
  1. function textToLink($subject){
  2.         $data = array();
  3.         $pattern = "/(?i)\b(((http|https|ftp):\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
  4.         $text = preg_replace($pattern, "<a href=\"$1\" target=\"_blank\">$1</a>", $subject);
  5.         $text = str_replace("href=\"www.","href=\"http://www.",$text);
  6.         return $text;
  7.     }

Esa funcion anda bien de esta manera

Código PHP:
Ver original
  1. $subject = 'link1 http://www.youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
  2.             link2 www.youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
  3.             link3 https://www.youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
  4.             link4 ftp://youtube.com/watch?v=CkSdiBltgYo&feature=fvwrel<br>
  5.                        link5 snipplr.com/view/64247/';
  6. $texto = textToLink($subject);

Funciona bien para los 4 primeros links, pero para el 5 retorna

Código HTML:
Ver original
  1. <a href="snipplr.com/view/64247/" target="_blank">snipplr.com/view/64247/</a>
y necesito que me retorne

Código HTML:
Ver original
  1. <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 original
  1. preg_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
__________________
Haz preguntas inteligentes-Como ser Hacker
No hacer preguntas por mensaje privado. No sólo no es inteligente sino que es egoísta.