ulthane — 2012-02-05T09:40:52-05:00 — #1
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?
ulthane — 2012-02-05T11:09:17-05:00 — #2
ulthane — 2012-02-05T12:04:30-05:00 — #3
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="www.something.com">www.something.com</a> back to www.something.com
cups — 2012-02-05T19:20:22-05:00 — #4
I am getting confused.
You have a textarea where users paste links
At what point do you convert them into links with tags around them then?
ulthane — 2012-02-06T04:52:25-05:00 — #5
When they post the form, the links go through the function i posted above (http://zenverse.net/php-function-to-auto-convert-url-into-hyperlink/) 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.
cups — 2012-02-06T07:00:31-05:00 — #6
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.
ulthane — 2012-02-06T07:52:58-05:00 — #7
Hey cups, thanks for the tips!
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
ulthane — 2012-02-06T08:28:40-05:00 — #8
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)
will leave us with this: blabla.com
How can i achieve this? thanks.
cups — 2012-02-06T10:38:32-05:00 — #9
ulthane — 2012-02-06T10:59:16-05:00 — #10
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="blabla.com">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="blabla.com">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.
ulthane — 2012-02-06T13:10:49-05:00 — #11
finally done it for these who want solution:
$string = htmlspecialchars_decode($string);
$string = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $string);
$needle = array('<a href="','" rel="nofollow"></a>');
$string = str_replace($needle,"",$string);