Posted By

rwunsch on 11/09/11


Tagged

url link String auto text autolink


Versions (?)

auto link urls in text / string


 / Published in: PHP
 

  1. function Xauto_link($str, $attributes=array()) {
  2. $attrs = '';
  3. foreach ($attributes as $attribute => $value) {
  4. $attrs .= " {$attribute}=\"{$value}\"";
  5. }
  6. $str = ' ' . $str;
  7. $str = preg_replace(
  8. '`([^"=\'>])(((http|https|ftp)://|www.)[^\s<]+[^\s<\.)])`i',
  9. '$1<a href="$2"'.$attrs.'>$2</a>',
  10. $str
  11. );
  12. $str = substr($str, 1);
  13. $str = preg_replace('`href=\"www`','href="http://www',$str);
  14. // fügt http:// hinzu, wenn nicht vorhanden
  15. return $str;
  16. }

Report this snippet  

You need to login to post a comment.