URL2Link – Detect URL and Convert to Link


Hits: 2927  

URL2Link() is a function to change an email address or URL or FTP into a clickable HTML hyperlink using eregi_replace().

<?php 

function URL2Link($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 = "me@svnlabs.com"; 
echo URL2Link($text); 

echo "<br /><br />"; 

// URL example 
$text = "http://www.svnlabs.com"; 
echo URL2Link($text); 

echo "<br /><br />"; 

// FTP URL example 
$text = "ftp://ftp.svnlabs.com"; 
echo URL2Link($text); 

?>


You can’t drag the horse to water and make it drink…

Published by

Sandeep Verma

I’m an Entrepreneur. I’m proud to work as Blogger, LAMP Programmer, Linux Admin, Web Consultant, Cloud Manager, Apps Developer, Searcher. Concentrate > Observe > Imagine > Launch