/ Published in: PHP
URL: http://www.misteroneill.com
This is a simple function to parse a string and turn any URLs it finds into links. Adds a rel attribute to the link with a value of "nofollow" by default.
Expand |
Embed | Plain Text
function replace_urls($string, $rel = 'nofollow'){ $host = "([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]"; $port = "(:\d{1,})?"; $path = "(\/[^?<>\#\"\s]+)?"; $query = "(\?[^<>\#\"\s]+)?"; return preg_replace("#((ht|f)tps?:\/\/{$host}{$port}{$path}{$query})#i", "<a href=\"$1\" rel=\"{$rel}\">$1</a>", $string); }
You need to login to post a comment.
