auto link urls in text / string


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.