Only links with http are supported. Https is not.

As a quick change you can do this:

Change

if (substr($freelink,0,7) == 'http://') {
$replacement = '<a href="' . $freelink . '">' . $phrase . '</a>';
}

to

if ( (substr($freelink,0,7) == 'http://') || (substr($freelink,0,8) == 'https://') ) {
$replacement = '<a href="' . $freelink . '">' . $phrase . '</a>';
}

Comments

eafarris’s picture

Assigned: Unassigned » eafarris

Even better would be a more generic solution to handle any valid url, like ftp:// and mailto: schemes in addition to http[s]:// .

eafarris’s picture

Committed version 1.6 to HEAD, supporting http, mailto, https, and ftp uri schemes. Please verify and close.

rar’s picture

your code is ok
> if (preg_match('/^(http|mailto|https|ftp):/', $freelink)) {

How about
if (preg_match('/^(\w+):\/\//',$freelink)

which avoids
1) hardcoding in protocols
2) having 5N operations for N matches.
3) matching non link text like "http: The definition of http is...."

Note: the above code is untested.

eafarris’s picture

I like this, but there are exceptions to "word colon slash slash": the "mailto:" protocol does not require (and, in fact, i've never seen) the double slashes. and "file:" requires (i think) three slashes. It can be easy, good, or correct: pick two. I think the current code is "easy, and "good," so I'll leave it how it is for now (that is, not "correct"), and wait for people to complain about not having "feed://" or "rtsp://" or "somethingelse:" protocol handlers.

A quick searching on google and safari.oreilly.com shows some very intricate regexps to attempt to handle URI schemes. Bleh. At this time, I don't want a complex regexp there, more correct though it may be. If only there was an is_url() function! :)

Anonymous’s picture