Forums | Mahara Community

Developers /
Automatically create links from URLs in text


anonymous profile picture
Deleted user
Posts: 117

13 October 2009, 1:50 PM

Before I start looking for a Smarty plugin to do this, is there a function in Mahara that will automatically create links from URLs in a string when parsing a template? No other BBCode or HTML would be allowed in the string.  I just want to convert the URLs to working hyperlinks and not touch any of the other text. 

I did find the autolink_text function in web.php, but it doesn't always work correctly.  For example, if a URL is on a line by itself, the link isn't created unless the link is the very first line.  Sometimes when there are two URLs in a row, it fails to even display one of them at all.

Any suggestions?  Thanks

anonymous profile picture
Deleted user
Posts: 1643

13 October 2009, 8:49 PM

Hi. I would have pointed you to that function too, while noting that the text should already be html escaped. Also, it expects that there's no <a href...> tags in there (even if they're escaped), as that function unconditionally links URLs that it finds.

I wonder if you could fix that problem you found by changing the preg_replace modifier to be 'me' instead of 'e'. (The 'm' modifier makes ^ and $ match at \n's in the string). I'm not sure about the second problem, though if you gave me an example string of text I could look into it for you.

Though, a question: given the limitations I described above, do you think a fixed version of that function will work for you? If not, then you're probably best off looking elsewhere, as Mahara has nothing else that'll help you (unless you use clean_html, as htmlpurifier does the autolinking for our HTML text). 

anonymous profile picture
Deleted user
Posts: 117

13 October 2009, 10:32 PM

Changing the preg_replace modifier to 'me' seemed to do the trick.  It appears to have fixed all the issues (hopefully). 

Thanks Nigel!

anonymous profile picture
Deleted user
Posts: 1643

13 October 2009, 11:10 PM

Awesome - I'll do that on master too Cool
4 results