I have a simple commenting system where people can submit hyperlinks inside the plain text field. When I display these records back from the database and into the web page, what RegExp in PHP can I use to convert these links into HTML-type anchor links?
I don't want the algorithm to do this with any other kind of link, just http and https.
Well, Volomike's answer is much closer. And to push it a bit further, here's what I did for it to disregard the trailing period at the end of the hyperlinks. I also considered URI fragments.
Here is my code to format all the links inside text, including emails, urls with and without protocol.
Please comment the url that doesn't work. I'll try to update the regex.
I am using a function that originated from question2answer, it accepts plain text and even plain text links in html:
A bit much code due to accepting links that hold brackets and other characters, but probably it helps.
I recommend not to do many things on fly like this. I prefer to use simple editor interface like the one used in stackoverflow. It is called Markdown.
If am right, what you want to do is turn ordinary text into http links. Here's what I think can help: