Welcome Guest, Not a member yet? Register   Sign In
wrap anchor tags around links in the database
#1

[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?
#2

[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.
}
#3

[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?
#4

[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!
#5

[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?
#6

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

http://ellislab.com/codeigniter/user-gui...elper.html
#7

[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.

#8

[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.
#9

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB