CodeIgniter Forums
wrap anchor tags around links in the database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: wrap anchor tags around links in the database (/thread-51093.html)



wrap anchor tags around links in the database - El Forum - 04-20-2012

[eluser]brian88[/eluser]
Say I have a input field named "bodyText"

users enter something like this into the input field, "Hey check out this awesome link http://www.kickasshumor.com"

Now how can I wrap <a> tags around it to make it look like this when it gets inserted into the database?

Code:
Hey check out this awesome link <a href="http://www.kickasshumor.com">http://www.kickasshumor.com</a>

And is it possible to tell if the link is from youtube, then just embed that youtube link on my page?


wrap anchor tags around links in the database - El Forum - 04-20-2012

[eluser]CroNiX[/eluser]
I wouldn't. I'd store the raw url and wrap the <a> tags around it when displaying it in the view.
Code:
if (strpos(strtolower($url), 'youtube.com') !== FALSE)
{
  //$url is from youtube.
}



wrap anchor tags around links in the database - El Forum - 04-20-2012

[eluser]brian88[/eluser]
Well the string might be a lot of text. The user could insert a paragraph and several links.

Wouldn't your way just wrap the whole paragraph in <a> tags?


wrap anchor tags around links in the database - El Forum - 04-20-2012

[eluser]InsiteFX[/eluser]
Ask your self this question how am I going to validate that the user entered a valid url links?

You would need to build a parser to check the input text and also to pull out all links to validate them.

If the user entered a link to an xxx site you could get sued!



wrap anchor tags around links in the database - El Forum - 04-23-2012

[eluser]brian88[/eluser]
That's true InsiteFX, I wont be going that far with this project. More for fun.

So Cronix suggested 1 way to detect the url. but that would be wrapping the whole paragraph in <a> tags. Not the link. Is there a better way?


wrap anchor tags around links in the database - El Forum - 04-23-2012

[eluser]Samus[/eluser]
try auto_link()

http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html


wrap anchor tags around links in the database - El Forum - 04-23-2012

[eluser]CroNiX[/eluser]
[quote author="brian88" date="1335191773"]That's true InsiteFX, I wont be going that far with this project. More for fun.

So Cronix suggested 1 way to detect the url. but that would be wrapping the whole paragraph in <a> tags. Not the link. Is there a better way?[/quote]No, my way only detected if it had 'youtube.com' in the text, which was one of the things you asked. It doesn't wrap anything in tags.




wrap anchor tags around links in the database - El Forum - 04-23-2012

[eluser]CroNiX[/eluser]
The challenge with what you are doing is you have no way of knowing whether the person would enter a link like:
Code:
<a href="http://www.some-site.com">Some Site</a>
http://www.some-site.com
www.some-site.com
some-site.com
So, there isn't a simple answer.


wrap anchor tags around links in the database - El Forum - 04-23-2012

[eluser]brian88[/eluser]
thanks samus. auto_link() works great! I didnt know there was such a thing. Thank you too cronix


wrap anchor tags around links in the database - El Forum - 04-23-2012

[eluser]CroNiX[/eluser]
auto_link() would fail in my 4th example (some-site.com) if it doesn't have a www in front, which a lot of sites don't nowadays.