SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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?

    thanks,
    ulthane

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

    thanks.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I am getting confused.

    You have a textarea where users paste links

    say ...
    Code:
    <textarea name=links>
    www.link1.com
    www.link2.com
    www.link3.com
    </textarea>
    At what point do you convert them into links with tags around them then?

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When they post the form, the links go through the function i posted above (http://zenverse.net/php-function-to-...nto-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.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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="blabla.com">blabla.com</a>
    will leave us with this: blabla.com

    How can i achieve this? thanks.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Did you try strip_tags?

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    finally done it for these who want solution:
    Code PHP:
    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;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •