wrap anchor tags around links in the database |
[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?
[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)
[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?
[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!
[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?
[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.
[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>
[eluser]brian88[/eluser]
thanks samus. auto_link() works great! I didnt know there was such a thing. Thank you too cronix
[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. |
Welcome Guest, Not a member yet? Register Sign In |