Jump to content

Insert link into text db field


son

Recommended Posts

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

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

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

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

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

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

Understood. But how would you accomplish it? Especially the email link is important (which actually is bit different)...Son
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.

Link to comment
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...