Jump to content

Ideas on converting text to a link with mysql and php


CStrauss

Recommended Posts

Here is my current project I been playing with and just want some ideas or suggestions to accomplish this taskBasicly I have a form I can type out a little article or what not, and when I submit the form it inserts the data into a database. Here is what I want to know. What is the best practice to storing information into a database so things that should be displayed as html when extracted show up on my page as html. for example if i type in my textarea of my form, a link refrence such as http://something.com or www.something.com, or http://www.something.com how should that be stored in a database and how can i extract it so it converts it to a link when displayed on my web page.I learned so far to use nl2br to help store nl as line breaks and I even made a function to convert them back with a custom made function called br2nl if needed to. So basicly Im looking for other functions in php i can use to do the same with other html entiies. Should i be using the htmlspecialcharacters or htmlentities function if so can some one give me a little bit more understandable explaintion then what the php online manual explains them as so i can understand those better if thats what i need to be using to complete what i want to try. Again its not with links im only looking for to convert but thats the main reason of this post just any html tags in general that wil be generated dynamicly on my page as its pulled from the database how should i be inserting this information into the database so i dont get garbage put in my database.Thanks for any input oyu can advice on

Link to comment
Share on other sites

well you can just search and replace to make URLs links...you will search for (http://|www\.)(.*)that will find http:// or www. and then every character that comes after it until a space.then you can just replace it with <a href='(1)'>(1)</a>that maybe how it goes, I don't do this alot... only once to tell you the truth. Justsomeguy will know how to do it better than I do. Well this should give you some ideas untill he posts.
Link to comment
Share on other sites

yeah i figure i might have to do that for now use search to replace but i was just curious if there were functions or special ways to input it the data and retrieve it but if anyone else knows better ways to do this please post and let me know so i can experment with it :)

Link to comment
Share on other sites

I wrote this function for converting a typed-in link to a fully-qualified URL. You can type in domain.com or www.domain.com or whatever and get back something like http://www.domain.com that you can use in an anchor tag:

################################################################################ compose_address#   This function attempts to create a fully-qualified web address from the#   given string.  If a scheme is not present, http is assumed.###############################################################################function compose_address($str){  if ($str == "")	return false;  $retval = "";  $chunks = parse_url($str);  if ($chunks['host'] == "" && $chunks['path'] == "")	return false;  $retval .= ($chunks['scheme'] == "" ? "http" : $chunks['scheme']);  $retval .= ":" . ($chunks['scheme'] == "mailto" ? "" : "//");  $retval .= ($chunks['user'] != "" ? $chunks['user'] : "");  $retval .= ($chunks['pass'] != "" ? ":" . $chunks['pass'] : "");  $retval .= ($chunks['user'] != "" ? "@" : "");  $retval .= $chunks['host'] . $chunks['path'];  $retval .= ($chunks['query'] != "" ? "?" . $chunks['query'] : "");  $retval .= ($chunks['fragment'] != "" ? "#" . $chunks['fragment'] : "");  return $retval;}

I made this for a 'URL' field on a form though, so it's not for finding and replacing things, it's for converting a string of text that you know to be an attempt at typing in an address to be a fully-qualified URL.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...