Insert link via textarea

Hey everyone,
I ran into a small problem, im looking for the easiest way to let users insert links through a textarea, can some1 give me a piece of code or example that does such a thing?


Never mind! found the solution:

Sorry for another post…
The solution i gave above works just fine, but when i come to update a record for example (again, via a textarea) the info will be pulled from the DB on that textarea with the <a href… tag etc
the problem is that if ill post the form, all the tags will be converted because im using htmlspecialchars function for security purposes, and the link will no longer work

so now i need help building a function that does the opposite of the function above, can someone help me with that please?

For example from <a href=“”></a> back to


I am getting confused.

You have a textarea where users paste links

say …

<textarea name=links>

At what point do you convert them into links with tags around them then?

When they post the form, the links go through the function i posted above ( and get inserted to the database with the tags around them.

I got another form where users can edit the links they inserted, so when they go to that form, the links are pulled from the DB to the text area

the problem is that they are pulled back with tags, i want them to be converted back to link only. so basically, the opposite of the above function.

Ok, now I understand.

Lets say the “raw” data is what they typed into the textarea, and the “transformed” data is after you have turned what looks like links into html links.

Take the “raw” input from the textarea, escape it properly and then store that in your database table.

Then, do the “transform” only when the data is to be displayed.

That’s probably fine if you are not on a busy site.

If you are on a busy site, or want to save some processor cycles – you have a couple of options – these are likely the most popular:

a) Store two versions of the data in your table “raw” and “transformed”.

When you want to display the data for the public, use the “transformed” data, when the user wants to edit their field, give them back the “raw” version.

Obviously when the users edits the raw data you update the transformed data too.

b) store the raw version in your db, and create a “cached” html file with the transformed data that you serve up from the file system using the include function – when you detect that the raw version has been changed, overwrite the cached file too.

You likely need to keep a note of the filename of the cached version with you raw version, so that you can look up its address.

Hey cups, thanks for the tips! :slight_smile:
My website is not busy, but i still preffer to avoid using functions and such when displaying, to decrease the load time as much as possible
I’ll go for storing both raw and transformed data into the DB, thanks :slight_smile:

Bah, i was sure that i saved the links on a seperated column, but i didnt, maybe i should’ve mentioned earlier that the textarea doesnt contain links only, it also contain text
it can also contain more than just 1 link
So i think that the only solution is to build up a function that reverse the transform function…
Or maybe do something like this: a function that will search for “<a” start tag, then go through it and delete everything till it meets an “>”, and from here it’ll delete the ending “</a>” tag
So for example from a link like this: (parts in red are to be deleted)
<a href=“”></a>
will leave us with this:

How can i achieve this? thanks.

Did you try [fphp]strip_tags[/fphp]?

Yes that did what i required above but i’ve changed my code, sorry for not updating here
I’ve done it so links will appear like that: <a href=“”>page title</a>
So the above function is not what i need anymore…
I need some function that will remove everything except for the string inside the quotes
So in this example: (stuff in red are to be removed)
<a href="">page title</a>
Will leave us with the lnik only.

Sorry for the confusion, any idea on how to achieve that?
Just a note to remmember, there might be multiple links and it has to start with “<a” and end with “</a>” to avoid deleting quotes that belong to actuall text

Thanks for the help.

finally done it :slight_smile: for these who want solution:

function reverse_clickable($string)
	$string = htmlspecialchars_decode($string);
	$string = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $string);
	$needle = array('<a href="','" rel="nofollow"></a>');
	$string = str_replace($needle,"",$string);
	return $string;