Ver Mensaje Individual
  #8 (permalink)  
Antiguo 06/10/2011, 20:40
Avatar de andresdzphp
andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 9 meses
Puntos: 793
Respuesta: Expresiones regulares

Bueno hay te la adapté capo, espero que te sirva. Si algo comentas y la seguimos mirando a ver como queda:

Código PHP:
Ver original
  1. <?php
  2. function texto_a_url($text) {
  3.     $ret = ' ' . $text;
  4.    
  5.     $patrones = array(
  6.     '#([\t\r\n ])([a-z0-9]+?){1}://([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?\.(png|jpg|gif))#i',
  7.     '#([\t\r\n ])([a-z0-9]+?){1}://([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i');
  8.     $reemplazos = array('\1<img src="\2://\3" />',
  9.     '\1<a href="\2://\3" target="_blank">\2://\3</a>');
  10.     $ret = preg_replace($patrones, $reemplazos, $ret);
  11.    
  12.     $ret = preg_replace("#([\t\r\n ])(www|ftp)\.(([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i",
  13.     '\1<a href="http://\2.\3" target="_blank">\2.\3</a>', $ret);
  14.    
  15.     $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i",
  16.     "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
  17.    
  18.     $ret = substr($ret, 1);
  19.    
  20.     return ($ret);
  21. }
  22.  
  23. $str = 'http://upload.wikimedia.org/wikipedia/commons/8/87/William_Boyce4.png http://www.google.com';
  24.  
  25. echo texto_a_url($str);

Saludos.
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP

Última edición por andresdzphp; 06/10/2011 a las 22:36 Razón: Mejora del código