Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
1.4k views
in Q2A Core by
With recent q2a v1.6.2 and before, all links that are like the example here (http://www.nytimes.com) get parsed with the ")" in the end.

I often have false link this way in my forum.

Can we just add to the core that a final ")" is not part of the link, that would solve a lot of dead links in all 10,000+ q2a forums.

Kai
Q2A version: 1.6.2

2 Answers

+2 votes
by
selected by
 
Best answer

I fixed this and it will be rolled into Q2A 1.6.3. Change the return line in function qa_html_convert_urls(...) in qa-app-format.php to:

return substr(preg_replace('/([^A-Za-z0-9])((http|https|ftp):\/\/([^\s&<>\(\)\[\]"\'\.])+\.([^\s&<>\(\)\[\]"\']|&amp;)+)/i', '\1<a href="\2" rel="nofollow"'.($newwindow ? ' target="_blank"' : '').'>\2</a>', ' '.$html.' '), 1, -1);

It now disallows all brackets [ ] ( ) in URLs.

by
seems to work, thanks a lot.

I asked for a solution here http://stackoverflow.com/questions/20198654/automatic-link-conversion-with-preg-replace-but-without-last-bracket-period-a but did not get an answer. I posted yours. Glad to see you found one! great work :)
by
Just found out that links with a period in the end do not work either. The period is added to the link, e.g. http://matharguments180.blogspot.de/2014/01/jan-20-is-zero-odd-or-even.html. Can we fix this as well?
0 votes
by
edited by

I believe it is technically impossible to actually fix this. The simplest example is that URLs can contain parentheses. So http://www.something.com/more) is actually a valid URL. So if the URL actually includes the closing parentheses or not depends on what the user is trying to link and you should use the editor to remove ambiguity.

However, you can infer that if there is a closing parentheses at the end of the text and there is no matching parentheses inside the URL such as wikipedia's URLs http://www.wikipedia.org/Computers_(Software), then the closing parentheses would, most likely, not be part the URL. So the logic is more complex that just replacing, as the current one is doing.

Here is the result of the parsing in 1.6.3:

 
I've modified the parsing logic and now they all succeed (according to the previously explained parentheses rules, of course).
 
See the pull request with the fix I created here: https://github.com/q2a/question2answer/pull/39
...