rev 1.47 in CVS introduced this bug... put 'hello http://test.com/test.htm goodbye' into the text and bbcode will leave the "m" out of the URL for the link. Looks like (?!\s) in the regexp is intended to not include a comma in the URL when that comma is followed by a space.. but it omits any final character that is followed by whitespace.

Comments

naudefj’s picture

Please try to change (?!\s) to \b. If it solves the problem, I will commit it to CVS.

mindless’s picture

I think putting a \b there would make a URL followed by ) or < to no longer match.

How about this?

preg_replace('#(?<=^|[\t\r\n >\(])([a-z]+?://[\w-.]+\.([\w-]+\.)*\w+(:[0-9]+)?(/[^ "\'\(;\n\r\t<\)]*)?)((?\1', $ret);

naudefj’s picture

Assigned: Unassigned » naudefj

I've tested the \b solution and it seems to work just fine. If there is objection, I will commit it to CVS.

mindless’s picture

yikes, I should have previewed my comment.. forgot < and > would mess it up.

Ok, I now see how \b matches so it will prevent a trailing comma from being included. \b will actually prevent any trailing punctuation or other \W from being included, so something like a://b.c/d?e=f! won't include the ! Not sure if that's a problem.. so you can use \b or the more imposing ((?<!,)|(?!\s)) which says "last character is not a comma or next character is not whitespace".. this would allow any character but the designated ones to be at the end of the url.

Here is my version with \b

$ret = preg_replace('#(?<=^|[\t\r\n >\(])([a-z]+?://[\w\-]+\.([\w\-]+\.)*\w+(:[0-9]+)?(/[^ "\'\(;\n\r\t<\)]*)?)\b#i', '<a href="\1">\1</a>', $ret);

It simplifies the regexp a little bit so you just need \1 in the replacement. It also will match a url at the very beginning of the text.

mindless’s picture

One last try, maybe I won't get word wrapped:

<code>$ret = preg_replace('#(?<=^|[\t\r\n >\(])([a-z]+?://[\w\-]+\.([\w\-]+\.)*\w+(:[0-9]+)?(/[^ "\'\(;\n\r\t<\)]*)?)\b#i', '<a href="\1">\1</a>', $ret);

naudefj’s picture

Status: Active » Fixed
mindless’s picture

FYI, on our site I removed semicolon from the list of terminating characters for the URL.. this is because we have our own "nohtml" filter that encodes & as &amp; and then bbcode would treat this ; as the end of the URL.

naudefj’s picture

Probably a good idea. Can you submit a patch?

mindless’s picture

i just removed a single ; character in the regexp for xxx:// matches, line 194 of bbcode-filter.inc
i suppose some of the changes from line 194 could be applied to line 200 too.

here's another case to consider for \b at the end: test://test.test/test?x=1&y=
ie, a URL that gives y an empty value, and the URL is at the end of a line.

naudefj’s picture

I trust this will solve the problem - http://drupal.org/cvs?commit=35667

If not, please supply a patch.

mindless’s picture

Looks good, thanks!

Anonymous’s picture

Status: Fixed » Closed (fixed)