Jump to content

[SOLVED] Help with converting a string in to a hyperlink


kee2ka4

Recommended Posts

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

<?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

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.