Skip to content
Advertisement

How to add anchor tag to a URL from text input

I want to be able to take user inputted text in a comment field and check for URL type expression, and if it exists, add an anchor tag (to url) when the comment is displayed.

I am using PHP on the server-side, and Javascript (with jQuery) on client, so should I wait to check for URL until right before it is displayed? Or add the anchor tag before inserting it in the database?

so

JavaScript

becomes

JavaScript

Advertisement

Answer

First, a request. Don’t do this before writing the data to the database. Instead, do it before displaying the data to the end-user. This will cut down on all confusion, and will allow you more flexibility in the future.

One example found online follows:

JavaScript

And a much more thorough one from daringfireball.net:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement