Forums | Mahara Community

Developers /
Automatically create links from URLs in text


anonymous profile picture
Account deleted
Posts: 117

13 October 2009, 13:50

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
Account deleted
Posts: 1643

13 October 2009, 20:49

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
Account deleted
Posts: 117

13 October 2009, 22:32

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
Account deleted
Posts: 1643

13 October 2009, 23:10

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