define('SF_AUTO_LINK_RE', '~
( # leading text
<\w+.*?>| # leading HTML tag, or
[^=!:\'"/]| # leading punctuation, or
^ # beginning of line
)
(
(?:https?://)| # protocol spec, or
(?:www\.) # [url]www.*[/url]
)
(
[-\w]+ # subdomain or domain
(?:\.[-\w]+)* # remaining subdomains or domain
(?::\d+)? # port
(?:/(?:(?:[\~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path
(?:\?[\w\+%&=.;-]+)? # query string
(?:\#[\w\-]*)? # trailing anchor
)
([[:punct:]]|\s|<|$) # trailing text
~x');
function _auto_link_urls($text)
{
return preg_replace_callback(
SF_AUTO_LINK_RE,
create_function('$matches', '
if (preg_match("/<a\s/i", $matches[1]))
{
return $matches[0];
}
else
{
return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'">\'.$matches[2].$matches[3].\'</a>\'.$matches[4];
}
')
, $text);
}