URL2Link – Detect URL and Convert to Link


Hits: 3056  

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

13 thoughts on “URL2Link – Detect URL and Convert to Link”

  1. Hey! check how we are detecting link and text, don’t use link for link-text 😉

    (((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)

  2. Pingback: GVK Biosciences
  3. Pingback: good seedbox
  4. Hey very nice site!! Guy .. Excellent .. Amazing .. I’ll bookmark your
    web site and take the feeds also? I am glad
    to find numerous useful information here in the submit, we’d like develop
    extra techniques on this regard, thanks for sharing. . . .
    . .

  5. What’s Happening i am new to this, I stumbled upon this I
    have discovered It absolutely helpful and it has aided me out loads.

    I’m hoping to give a contribution & aid different customers
    like its helped me. Great job.

  6. Pretty section of content. I just stumbled upon your site and
    in accession capital to assert that I get actually
    enjoyed account your blog posts. Any way I will be subscribing to your augment and even I achievement you
    access consistently quickly.

Leave a Reply

Your email address will not be published.

*