elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  Extraer urls de un string y darle formato html
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Extraer urls de un string y darle formato html  (Leído 1,989 veces)
Littl3

Desconectado Desconectado

Mensajes: 239


Tarde o temprano aparece el hombre


Ver Perfil
Extraer urls de un string y darle formato html
« en: 17 Junio 2009, 02:27 am »

Hola buenas,

Estoy tratando de hacer un script que haga lo siguiente:

Código
  1. $string='el foro de www.foro.elhacker.net es un foro muy bueno y su pagina web es
  2. www.elhacker.net y con http es http://elhacker.net o http://www.elhacker.net';
  3.  
  4. $stringformateada = formatearurls($string);
  5.  
  6. echo $stringformateada;

Y esto tendria que devolver:

Citar
el foro de www.foro.elhacker.net es un foro muy bueno y su pagina web es
www.elhacker.net y con http es http://elhacker.net o http://www.elhacker.net

Como veis hay varios formatos de url... por eso necesito un script bastante completo y de momento he sido incapaz de hacerlo....

EDITO: ya tengo el script y funciona a la perfeccion lo pongo por si alguien lo busca algun dia xD...


Código
  1. <?php
  2.  
  3. /**
  4. * Rewritten by Nathan Codding - Feb 6, 2001.
  5. * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
  6. * to that URL
  7. * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
  8. * to http://www.xxxx.yyyy[/zzzz]
  9. * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
  10. * to that email address
  11. * - Only matches these 2 patterns either after a space, or at the beginning of a line
  12. *
  13. * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
  14. * have it require something like xxxx@yyyy.zzzz or such. We'll see.
  15. */
  16. function make_clickable($text)
  17. {
  18.  
  19. // pad it with a space so we can match things at the start of the 1st line.
  20. $ret = ' ' . $text;
  21.  
  22. // matches an "xxxx://yyyy" URL at the start of a line, or after a space.
  23. // xxxx can only be alpha characters.
  24. // yyyy is anything up to the first space, newline, comma, double quote or <
  25. $ret = preg_replace("#([\t\r\n ])([a-z0-9]+?){1}://([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i", '\1<a href="\2://\3" target="_blank">\2://\3</a>', $ret);
  26.  
  27. // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
  28. // Must contain at least 2 dots. xxxx contains either alphanum, or "-"
  29. // zzzz is optional.. will contain everything up to the first space, newline,
  30. // comma, double quote or <.
  31. $ret = preg_replace("#([\t\r\n ])(www|ftp)\.(([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)#i", '\1<a href="http://\2.\3" target="_blank">\2.\3</a>', $ret);
  32.  
  33. // matches an email@domain type address at the start of a line, or after a space.
  34. // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
  35. $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
  36.  
  37. // Remove our padding..
  38. $ret = substr($ret, 1);
  39.  
  40. return($ret);
  41. }
  42.  
  43. $text='el foro de www.foro.elhacker.net es un foro muy bueno y su pagina web es
  44. www.elhacker.net y con http es http://elhacker.net o http://www.elhacker.net';
  45.  
  46. echo make_clickable($text);
  47.  
  48.  
  49.  
  50.  
  51. ?>

Saludos


« Última modificación: 17 Junio 2009, 02:55 am por Littl3 » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
extraer titulo de link html
Programación Visual Basic
usuario oculto 3 3,802 Último mensaje 6 Agosto 2011, 17:31 pm
por AlxSpy
¿Extraer palabras de un string?
.NET (C#, VB.NET, ASP)
dont'Exist 3 4,397 Último mensaje 17 Marzo 2012, 14:32 pm
por dont'Exist
Extraer texto de html en bat ?
Scripting
novato04 2 2,837 Último mensaje 26 Agosto 2014, 12:15 pm
por Eleкtro
Extraer Palabra de un String con delimitadores
PHP
spysecurityca 4 3,610 Último mensaje 3 Septiembre 2014, 16:51 pm
por WHK
Extraer dato de String (JAVA)
Java
davidlynch 2 2,094 Último mensaje 7 Mayo 2015, 09:57 am
por Sr_Malweriks
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines