fork(1) download
  1. <?php
  2.  
  3. function MontarLink($texto)
  4. {
  5. if (!is_string ($texto))
  6. return $texto;
  7.  
  8. $er = "/(https:\/\/(www\.|.*?\/)?|http:\/\/(www\.|.*?\/)?|www\.)([a-zA-Z0-9]+|_|-)+(\.(([0-9a-zA-Z]|-|_|\/|\?|=|&)+))+/i";
  9.  
  10. preg_match_all ($er, $texto, $match);
  11.  
  12. foreach ($match[0] as $link)
  13. {
  14.  
  15. //coloca o 'http://' caso o link não o possua
  16. $link_completo = (stristr($link, "http") === false) ? "http://" . $link : $link;
  17.  
  18. $link_len = strlen ($link);
  19.  
  20. //troca "&" por "&", tornando o link válido pela W3C
  21. $web_link = str_replace ("&", "&amp;", $link_completo);
  22. $texto = str_ireplace ($link, "<a href=\"" . strtolower($web_link) . "\" target=\"_blank\">". (($link_len > 60) ? substr ($web_link, 0, 25). "...". substr ($web_link, -15) : $web_link) ."</a>", $texto);
  23.  
  24. }
  25.  
  26. return $texto;
  27.  
  28. }
  29.  
  30. echo MontarLink("ola mundo www.cade.com.br bla bla www.foo.com");
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
ola mundo <a href="http://w...content-available-to-author-only...m.br" target="_blank">http://w...content-available-to-author-only...m.br</a> bla bla <a href="http://w...content-available-to-author-only...o.com" target="_blank">http://w...content-available-to-author-only...o.com</a>