son Posted August 5, 2008 Share Posted August 5, 2008 Have this text input field, which inserts the data into text field in db. Is there a way that when I enter a url that it stores it as a url and retrieves it this way. That would be great...Son Link to comment Share on other sites More sharing options...
Ingolme Posted August 5, 2008 Share Posted August 5, 2008 Well, you can store the URL, and then when you retrieve it you put it into an <a> element.Something like: $query = mysql_query("SELECT url,text FROM table");while($r = mysql_fetch_array($query)) {echo "<a href=\"{$r['url']}\">{$r['text']}</a>";} Link to comment Share on other sites More sharing options...
son Posted August 5, 2008 Author Share Posted August 5, 2008 Well, you can store the URL, and then when you retrieve it you put it into an <a> element.Something like:$query = mysql_query("SELECT url,text FROM table");while($r = mysql_fetch_array($query)) {echo "<a href=\"{$r['url']}\">{$r['text']}</a>";} Sorry, Ingolme. Guess I did not explain properly.The link could be part of the normal text, so if I enter "I like the website about cms, which can be found under www.domain.co.uk." I would like that 'www.domain.co.uk' is either entered as link into db or on page where text is displayed it is put into the correct link. Not sure how to achieve my objective...Son Link to comment Share on other sites More sharing options...
Ingolme Posted August 5, 2008 Share Posted August 5, 2008 Well, it's usually done after being retrieved from the database, something like this: //Retrieve a string from the database and put it into $str for this examplepreg_replace("/(http:\/\/[^\s<,:;]*)\b/","<a href=\"$1\">$1</a>",$str); I'm not the best expert in regular expressions yet, if somebody else has a better one, they can tell me. Link to comment Share on other sites More sharing options...
son Posted August 5, 2008 Author Share Posted August 5, 2008 Well, it's usually done after being retrieved from the database, something like this://Retrieve a string from the database and put it into $str for this examplepreg_replace("/(http:\/\/[^\s<,:;]*)\b/","<a href=\"$1\">$1</a>",$str); I'm not the best expert in regular expressions yet, if somebody else has a better one, they can tell me. That's great and working fine:-) Can can I combine two regular expression to also test for links just starting with www and find email links, which obviously need to have 'mailto:' bit in front? It is all regarding the same text retrieved from db...Son Link to comment Share on other sites More sharing options...
Ingolme Posted August 5, 2008 Share Posted August 5, 2008 Watch out with using a second regular expression, it will take all the links with "www." even if this one processed them earlier. Link to comment Share on other sites More sharing options...
son Posted August 5, 2008 Author Share Posted August 5, 2008 Watch out with using a second regular expression, it will take all the links with "www." even if this one processed them earlier.Understood. But how would you accomplish it? Especially the email link is important (which actually is bit different)...Son Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2008 Share Posted August 5, 2008 You can do a Google search to find some common regular expressions, there are patterns to match email addresses, URLs, etc. Link to comment Share on other sites More sharing options...
son Posted August 5, 2008 Author Share Posted August 5, 2008 You can do a Google search to find some common regular expressions, there are patterns to match email addresses, URLs, etc.I found plenty of examples, I just do not know how to combine url and email check them for $str (which is variable, which holds text retrieved from db)...Son Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2008 Share Posted August 5, 2008 I'm not sure why you want to combine the two, each of them gets replaced with a different thing so it makes more sense to me to do one, then the other. Link to comment Share on other sites More sharing options...
Ingolme Posted August 6, 2008 Share Posted August 6, 2008 Understood. But how would you accomplish it? Especially the email link is important (which actually is bit different)...SonI think this won't interfere with the other one:preg_replace("/\b(www.[^\s<,:;]*)\b/","<a href=\"http://$1\">$1</a>",$str); There are possibly more efficient ways than this if you search google, I'm just doing it from my knowledge. Link to comment Share on other sites More sharing options...
son Posted August 8, 2008 Author Share Posted August 8, 2008 I think this won't interfere with the other one:preg_replace("/\b(www.[^\s<,:;]*)\b/","<a href=\"http://$1\">$1</a>",$str); There are possibly more efficient ways than this if you search google, I'm just doing it from my knowledge. Thanks to both of you (sorry ro late reply, my internet was cut off for two days). Will have a go...Just one more question (I know I am a pain;-)): It is possible that when the user selects the word he/she wants to have as hyperlink he/she can manually add the link, which is stored in db? Sometimes there are occasions where you do not want to see the hyperlink in text, but you want a word to act as hyperlink. If it is possible, what is the term to search for in Google?Son Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.