kee2ka4 Posted June 15, 2008 Share Posted June 15, 2008 Hello everyone, I need some help to convert a string and display it as a hyperlink on a page. Bascially I have a "comments" field in a database that is of type "text". When I display the string on a page, it doesn't display hyperlink text as a hyperlink. So say for example I have a comment that says "Go and check out this link: http://domain.com", The page will display this as a simple text without no hyperlink. How can I display http://domain.com as a hyperlink. Is there any function over there that would help me solve my problem? Thanks, Ket Link to comment https://forums.phpfreaks.com/topic/110283-solved-help-with-converting-a-string-in-to-a-hyperlink/ Share on other sites More sharing options...
papaface Posted June 15, 2008 Share Posted June 15, 2008 <?php function makeClickableLinks($text) { $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text); $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text); $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text); return $text; } // Usage // Email address example $text = "[email protected]"; echo makeClickableLinks($text); echo "<br /><br />"; // URL example $text = "http://www.example.com"; echo makeClickableLinks($text); echo "<br /><br />"; // FTP URL example $text = "ftp://ftp.example.com"; echo makeClickableLinks($text); ?> Google is amazing Link to comment https://forums.phpfreaks.com/topic/110283-solved-help-with-converting-a-string-in-to-a-hyperlink/#findComment-565875 Share on other sites More sharing options...
kee2ka4 Posted June 15, 2008 Author Share Posted June 15, 2008 Hey, thanks very much... works perfectly... Ket Link to comment https://forums.phpfreaks.com/topic/110283-solved-help-with-converting-a-string-in-to-a-hyperlink/#findComment-565910 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.