-2

I am developing a website in which user writes something in textarea. When the user clicks on submit the data is saved in the database. When I retreive the data, if the data contain a link it is not clickable. This is obviously because it is not enclosed in anchor tags.

Now I have two options:

  1. Put Anchor tags before saving data in database.
  2. Put anchor tags when data is retrieved from Database.

I have no idea how to do that. I am not using tinyMCE plugin.

If I compare result returned by database character by character then it will be also useless. I am in search of a good algorithm.

hakre
  • 193,403
  • 52
  • 435
  • 836
require_once
  • 1,995
  • 3
  • 21
  • 29
  • Could you paste the code you using to retrieve the links/info from the database? – Shattuck Jan 13 '12 at 20:42
  • This might help. It looks like the same thing was asked here. http://stackoverflow.com/questions/1959062/how-to-add-anchor-tag-to-a-url-from-text-input – Shattuck Jan 13 '12 at 20:43
  • @mike Can you please tell me why you need that code.its a simple query select * from feedback. – require_once Jan 13 '12 at 20:44
  • Yes i saw that question but that doesnt helped me out – require_once Jan 13 '12 at 20:46
  • 1
    The question and answers above assume that you want to save the anchors into the database. If not, you can run the regex upon retrieving the text/links from the database. – Shattuck Jan 13 '12 at 20:56
  • how do i use regex while retrvng? – require_once Jan 13 '12 at 21:14
  • You would use it after retrieving. For example, if you were to retrieve it from the database and store it into a $text variable, you would then use the regex function on the $test variable. – Shattuck Jan 13 '12 at 21:19

1 Answers1

1

Follow these steps:

  • Scan the posted text with a regex to detect URLs ( Regex in preg_replace to detect url format and extract elements)
  • In case any URL is found, do a string replace and replace the url text with HTML code <a href="$url">$url</a>
  • Save to the database
  • Since you're not using any WYSIWYG text editor in client side, When editing you might want to strip the HTML tags out of your text before serve it, so the modified links become plain URL again.

An alternative solution would be to use javascript to create the links on the fly, following the same pattern above but this time on client side.

I Would choose the Javascript solution, and do just like most social applications do nowadays (Ex: Twitter).

Community
  • 1
  • 1
marcio
  • 10,002
  • 11
  • 54
  • 83
  • Can you show me how you use javascript with it? – require_once Jan 13 '12 at 21:12
  • @user1115580, this question has some good answers for javascript plain links replacement --> http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links good luck ;) – marcio Jan 13 '12 at 21:18
  • that didnt helped,i saw that before – require_once Jan 13 '12 at 21:35
  • See this specifit answer -> http://stackoverflow.com/a/7123542/438563, all you will need to do next is Loop trough the DOM elements where you want to add links, get the text from these elements and `yourText.linkify();`. I don't want to post the solution for Javascript link replacement because it was already posted many times in many answers. – marcio Jan 13 '12 at 22:10