Page 1 of 1

Replace urls with hyperlinks

Posted: Thu Jul 28, 2005 6:41 pm
by Todd_Z
Not the best, but an honest effort:

Code: Select all

function addLinks ( $str ) {
  $str = preg_replace( "#(http://(?:www\.)?[^\s\.]*\.[^\s$]+)#i" , "<a href=\"$1\" style=\"font-weight: bold;\">$1</a>", $str );
  echo preg_replace( "#([^/]www\.[\S$]*)#i" , "<a href=\"http://$1\" style=\"font-weight: bold;\">$1</a>", $str );
}
Critiques?

Posted: Thu Jul 28, 2005 6:49 pm
by Chris Corbyn
Yeah it certainly will work for most common URL's :D

I really want to write another one that actually checks for valid characters (and allows subdomains too) but I'm off to bed cos I'm tired.

In other words... yes it works.. good job ;) It will fail on subdomains and allows invalid characters too... fix that and it's brill.

URL patterns in the strictest sense can get hideously complex so only evaluate it as much as you need to (find the RFC's and have another go if you were feeling geeky enough and you'll see how complex it *can* be).

Good work ;)

Posted: Thu Jul 28, 2005 7:01 pm
by timvw

Posted: Thu Jul 28, 2005 7:19 pm
by Todd_Z
Haha, i have way to much work to complete to wade through those links - but thanks timvw!